顺便问下:什么情况下能用到引用相等呢?
当2个实例的引用相等的时候,如果改变一个,那么另外一个也会改变
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class ReferenceEqual
{
string temp;
static void Main()
{
ReferenceEqual re = new ReferenceEqual();
re.temp = "temp";
ReferenceEqual rere = re;
Console.WriteLine(re.temp );
Console.WriteLine(rere.temp );
Console.WriteLine(re.Equals (rere));
Console.WriteLine(ReferenceEquals (re,rere));
re.temp = "temptemp";
Console.WriteLine(re.temp);
Console.WriteLine(rere.temp);
Console.WriteLine(re.Equals(rere));
Console.WriteLine(ReferenceEquals(re, rere));
rere = new ReferenceEqual();
re.temp = "temptemptemp";
Console.WriteLine(re.temp);
Console.WriteLine(rere.temp);
Console.WriteLine(re.Equals(rere));
Console.WriteLine(ReferenceEquals(re, rere));
}
}
}
输出
temp
temp
True
True
temptemp
temptemp
True
True
temptemptemp
False
False
因此我们得出结论
1:==不要乱用,也不要随便重写
2:2个引用相等的时候,改变其一也会改变另外一个
3: ==是重写了Equal()
曾经的曾经已不在