ect)a == (object)b);//返回false,下面解释为什么是false。这个是引用类型'==',放到下文介绍
*/
3.1.4、结论:对于简单常见值类型 int、float、double、decimal等,Equal函数 and 运算符'==',如果其值相等,返回true;否则,返回false。
3.2、 结构体struct
3.2.1、 ValueType内部的Equals函数
ValueType
{
[System.Security.SecuritySafeCritical]
public override bool Equals (Object obj) {
BCLDebug.Perf(false, "ValueType::Equals is not fast. "+this.GetType().FullName+" should override Equals(Object)");
if (null==obj) {
return false;
}
RuntimeType thisType = (RuntimeType)this.GetType();
RuntimeType thatType = (RuntimeType)obj.GetType();
if (thatType!=thisType) {
return false;
}
Object thisObj = (Object)this;
Object thisResult, thatResult;
// if there are no GC references in this object we can avoid reflection
// and do a fast memcmp
if (CanCompareBits(this))
return FastEqualsCheck(thisObj, obj);
FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
for (int i=0; i<thisFields.Length; i++) {
thisResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(thisObj);
thatResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(obj);
if (thisResult == null) {
if (thatResult != null)
return false;
}
else
if (!thisResult.Equals(thatResult)) {
return false;
}
}
return true;
}
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool CanCompareBits(Object obj);
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern bool FastEqualsCheck(Object a, Object b);
}
3.2.2、结构体(只有值类型,重写Equal函数 and 运算符'==')
3.2.2.1、测试代码:
struct Point
{
public double x;
public double y;
public double z;
public Point(double X, double Y, double Z)
{
this.x = X;
this.y = Y;
this.z = Z;
}
public override bool Equals(Object obj)
{
if (!(obj is Point))
{
return false;
}
if (((Point)obj).x == this.x)
{
return true;
}
return false;
}
public bool Equals(Point obj)
{
if (obj.x == this.x)
{
return true;
}
return false;
}
//运算符“Point.operator ==(Point, Point)”要求也要定义匹配的运算符“!=”
public static bool operator ==(Point left, Point right)
{
return left.x == right.x;
}
public static bool operator !=(Point left, Point right)
{
return left.x != right.x;
}
}
Point p1 = new Point(1, 2, 3);
Point p2 = p1;
p1.y = 100;
Console.WriteLine(p1 == p2);//返回true
Console.WriteLine(p1.Equals(p2)); // 返回true
Console.WriteLine(p1.Equals((object)p2)); // 返回true
3.2.2.2、结论:此时程序执行我们重写的Equal函数 and 运算符'=='。
3.2.3、结构体(只有值类型,不重写Equal函数 and 运算符'==')
3.2.3.1、测试代码:
struct Point
{
public double x;
public double y;
public double z;
public Point(double X, double Y, double Z)
{
this.x = X;
this.y = Y;
this.z = Z;
}
}
Point p1 = new Point(1, 2, 3);
Point p2 = p1;
Console.WriteLine(p1 == p2);//编译错误:运算符"=="无法应用于"Point"和"Point"类型的操作数
Console.WriteLine(p1.Equals(p2)); // 返回true
Console.WriteLine(p1.Equals((object)p2)); // 返回true
p1.y = 100;
Console.WriteLine(p1.Equals(p2)); // 返回false
Console.WriteLine(p1.Equals((object)p2)); // 返回false
3.2.3.2、程序执行时,CanCompareBits(this)返回true,代码执行return FastEqualsCheck(thisObj