给出以下方法:
static void ChangeArray(params string[] array) {
for (int i = 0; i < array.Length; i++)
array[i] = array[i] + "s";
}
Run Code Online (Sandbox Code Playgroud)
如果我调用它传递一个字符串数组,这是有效的:
string[] array = {"Michael", "Jordan"} // will become {"Michaels", "Jordans"}
ChangeArray(array);
Run Code Online (Sandbox Code Playgroud)
但如果我使用字符串参数调用它将无法工作:
string Michael = "Michael";
string Jordan = "Jordan";
ChangeArray(Michael, Jordan); // This will NOT change the values of the variables
Run Code Online (Sandbox Code Playgroud)
我知道编译器会将Michael和Jordan包装在一个数组上,所以两种情况下结果不一样吗?
环境:C#6,Visual Studio 2015 CTP 6
给出以下示例:
namespace StaticCTOR
{
struct SavingsAccount
{
// static members
public static double currInterestRate = 0.04;
static SavingsAccount()
{
currInterestRate = 0.06;
Console.WriteLine("static ctor of SavingsAccount");
}
//
public double Balance;
}
class Program
{
static void Main(string[] args)
{
SavingsAccount s1 = new SavingsAccount();
s1.Balance = 10000;
Console.WriteLine("The balance of my account is \{s1.Balance}");
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
}
由于某种原因,静态ctor没有被执行.如果我将SavingsAccount声明为类而不是结构,它就可以正常工作.
c# static-constructor visual-studio c#-6.0 visual-studio-2015
给出以下代码:
#include <iostream>
using namespace std;
template<typename T> void Print(T t) {
cout << t << endl;
}
template<> void Print<int>(int t) {
cout << "int = " << t << endl;
}
void Print(int i) {
cout << "int2 = " << i << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Print(1.3);
Print("tese");
Print(2);
char c;
cin >> c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么调用Print(2)不是模糊的,而是调用void Print(int i)?
ps:使用bcc64.exe和cl.exe进行测试.
c++ templates overloading template-specialization overload-resolution
来自Bjarne Stroustrup的The C++ Programming Language第4版:
3.3.4.抑制操作
使用层次结构中的类的默认副本或移动通常是一个灾难:只给出指向基类的指针,我们根本不知道派生类有哪些成员(§3.2.2),所以我们无法知道如何复制它们.因此,最好的做法通常是删除默认副本和移动操作,即消除这两个操作的默认定义:
class Shape {
public:
Shape(const Shape&) =delete; // no copy operations
Shape& operator=(const Shape&) =delete;
Shape(Shape&&) =delete; // no move operations
Shape& operator=(Shape&&) =delete;
~Shape();
// ...
};
Run Code Online (Sandbox Code Playgroud)
为了试图理解他的意思,我创建了以下示例:
#include <iostream>
using namespace std;
class Person {
private:
int age;
public:
Person(const int& Age) : age {Age} {};
Person(const Person& from) : age {from.Age()} { cout << "copy constructor" << endl; };
Person& operator=(const Person& from) { cout << "copy assignment" << endl; …Run Code Online (Sandbox Code Playgroud) 如果我生成各种线程,并告诉他们所有使用相同的方法:
internal class Program {
private static DoSomething() {
int result = 0;
Thread.Sleep(1000);
result++;
int ID = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0} return {1}", ID, result);
}
private static Main() {
Thread[] threads = new Thread[50];
for (int i = 0; i < 50; i++)
threads[i] = new Thread(DoSomething);
foreach (Thread t in threads)
t.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
所有线程都会共享相同的堆栈吗?当我运行程序时,所有线程都返回1,所以我猜答案是否定的,但这是否意味着CLR在内存中制作了不同的方法副本?
给以下代码:
internal interface IHasLegs
{
int NumberOfLegs { get; }
}
internal interface IHasName
{
string Name { get; set; }
}
class Person : IHasLegs, IHasName
{
public int NumberOfLegs => 2;
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}
class Program
{
static void ShowLegs(IHasLegs i)
{
Console.WriteLine($"Something has {i.NumberOfLegs} legs");
}
static void Main(string[] args)
{
Person p = new Person("Edith Piaf");
ShowLegs(p);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现ShowLegs,以便它只接受实现IHasLegs和IHasName的值,而不必声明中间IHasLegsAndHasName:IHasLegs,IHasName?像ShowLegs((IHasLegs,IHasName)i){}之类的东西.
从C#类型转换表(http://msdn.microsoft.com/en-us/library/08h86h00.aspx):"缩小转换也可能导致其他数据类型的信息丢失.但是,OverflowException是如果正在转换的类型的值超出目标类型的MaxValue和MinValue字段指定的范围,则抛出,并且运行时检查转换以确保目标类型的值不超过其MaxValue或MinValue ".
所以我期待以下代码生成异常:
static void Main() {
int numb1 = 333333333;
short numb2 = (short)numb1;
Console.WriteLine("Value of numb1 is {0}", numb1);
Console.WriteLine("Type of numb1 is {0}", numb1.GetType());
Console.WriteLine("MinValue of int is {0}", int.MinValue);
Console.WriteLine("MaxValue of int is {0}\n", int.MaxValue);
Console.WriteLine("Value of numb2 is {0}", numb2);
Console.WriteLine("Type of numb2 is {0}", numb2.GetType());
Console.WriteLine("MinValue of short is {0}", short.MinValue);
Console.WriteLine("MaxValue of short is {0}", short.MaxValue);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
但相反,numb2的值为17237.我不知道这个值来自何处,我真的不明白为什么没有生成溢出异常.
任何建议都非常感谢!谢谢.