对于.NET中的值对象和引用对象在使用Equal方法中总是有区别的.从字面上也可以理解得出来,值对象使用Equal方法的时候,判断的是值对象的值是否相等,而引用对象使用Equal方法时是判断的是引用对象的内存地址是否相同.用一个测试来看看结论吧.
class Class1
{
[STAThread]
static void Main(string[] args)
{
test1 t1 = new test1();
test1 t2 = new test1();
test1 t3 = t1;
Console.WriteLine(t1.Equals(t2));
Console.WriteLine(t1.Equals(t3));
string s1 = "tt";
string s2 = "tt";
Console.WriteLine(s1.Equals(s2));
int i1 = 10;
int i2 = 10;
Console.WriteLine(i1.Equals(i2));
object o1 = (object)i1;
object o2 = (object)i2;
Console.WriteLine(o1.Equals(o2));
test2 t21 = new test2();
test2 t22 = new test2();
Console.WriteLine(t21.Equals(t22));
}
}
class test1
{
}
struct test2
{
}
结果:
False
True
True
True
True
True
Press any key to continue