Console.WriteLine((2 + 2) == 4);
object s = 1;
object t = 1;
Console.WriteLine(s == t);
string a = "hello";
string b = String.Copy(a);
string c = "hello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
Console.WriteLine((object)a == (object)c);
object s = 1;
object t = 1;
Console.WriteLine(s == t);
string a = "hello";
string b = String.Copy(a);
string c = "hello";
Console.WriteLine(a == b);
Console.WriteLine((object)a == (object)b);
Console.WriteLine((object)a == (object)c);
如下分析:
1.2+2 = 4没疑问
2.s==t 引用类型,比较内存地址,所以肯定不相同
3.a==b ?首先string。Copy方法是重新创建一个和原来的字符串值一样的对象,然而string是特殊的引用类型,==仅仅比较了值,故而“true'
4.(object)a == (object)b ?是因为a和b不是同一个对象,地址不一样,所以既然他们不再是string是object了,就自然不会走string那一套了,是 false
5.string a ="hello";
string c = "hello";
实际上a和c两个引用引用了内存的同一个对象(地址一样),地址一样,所以不管他们怎么包装,都是true