C#中判断两个对象是否相等有Equals、RefrenceEquals和==三种,其中==为运算符,其它两个为方法,而Equals又有两种版本,一个是静态的,一个是虚拟的,虚拟的可以被实体类重写,静态的在方法体内也是调用虚拟的,如下:
public
static
bool
Equals(
object
objA,
object
objB)
{
return ((objA == objB) || (((objA != null ) && (objB != null )) && objA.Equals(objB)));
}
public virtual bool Equals( object obj)
{
return InternalEquals( this , obj);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool InternalEquals( object objA, object objB);
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static bool ReferenceEquals( object objA, object objB)
{
return (objA == objB);
}
{
return ((objA == objB) || (((objA != null ) && (objB != null )) && objA.Equals(objB)));
}
public virtual bool Equals( object obj)
{
return InternalEquals( this , obj);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool InternalEquals( object objA, object objB);
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static bool ReferenceEquals( object objA, object objB)
{
return (objA == objB);
}
以上为reflector反编译的代码。它们的区别与联系总结如下:
1. RefrenceEquals顾名思义,在任何情况下都是判断两个对象的引用是否相等,对于值类型,因为每次判断前都必须进行装箱操作,也就是每次都生成了一个临时的object,因而永远返回false。String类型比较特殊,只要字符相同永远是同一个引用,字符不同就是不同的引用,即使通过传递赋值如:string str1 = "a"; string str2 = str1; str2 = "b";这时str1,str2依旧是不同引用。
2. ==与Equals并无本质区别,它们大多数情况下都是一样的,对于基本值类型,判断的是值是否相等,对于引用类型,判断的则是引用是否一样。值得注意的是,自定义的值类型struct,本身并不支持运算符==,强行使用将会出现编译错误。并且,鉴于Equals是虚方法,它可以被具体类重写,因此需要具体问题具体分析。
3. 前面提过,静态的Equals本质上也是调用虚拟的Equals,它们的区别在于调用时,虚拟的要考虑对象是否为空,否则会抛异常,而静态的则无需考虑。
以下为测试代码:
class
Program
{
static void Main( string [] args)
{
// AAA a1 = new AAA { Name = "a1", Age = 22 };
// AAA a2 = new AAA { Name = "a1", Age = 22 };
// int a1 = 123;
// int a2 = 123;
string a1 = " abc " ;
string a2 = " abc " ;
Console.WriteLine( string .Format( " ==: {0} " , a1 == a2));
Console.WriteLine( string .Format( " Equals: {0} " , a1.Equals(a2)));
Console.WriteLine( string .Format( " Static Equals: {0} " , Object.Equals(a1, a2)));
Console.WriteLine( string .Format( " ReferenceEquals: {0} " , ReferenceEquals(a1, a2)));
Console.Read();
}
}
// Class or Struct
struct AAA
{
public string Name { get ; set ; }
public int Age { get ; set ; }
}
{
static void Main( string [] args)
{
// AAA a1 = new AAA { Name = "a1", Age = 22 };
// AAA a2 = new AAA { Name = "a1", Age = 22 };
// int a1 = 123;
// int a2 = 123;
string a1 = " abc " ;
string a2 = " abc " ;
Console.WriteLine( string .Format( " ==: {0} " , a1 == a2));
Console.WriteLine( string .Format( " Equals: {0} " , a1.Equals(a2)));
Console.WriteLine( string .Format( " Static Equals: {0} " , Object.Equals(a1, a2)));
Console.WriteLine( string .Format( " ReferenceEquals: {0} " , ReferenceEquals(a1, a2)));
Console.Read();
}
}
// Class or Struct
struct AAA
{
public string Name { get ; set ; }
public int Age { get ; set ; }
}