class MyRefType : BaseType { RefType refobj; // This field is a reference type. ValType valobj; // This field is a value type. publicoverride Boolean Equals(Object obj) { // Because ’this’ isn’t null, if obj is null, // then the objects can’t be equal. if (obj ==null) returnfalse; // If the objects are of different types, they can’t be equal. if (this.GetType() != obj.GetType()) returnfalse; // Cast obj to this type to access fields. NOTE: This cast can’t // fail because you know that objects are of the same type. MyRefType other = (MyRefType) obj; // To compare reference fields, do this: if (!Object.Equals(refobj, other.refobj)) returnfalse; // To compare value fields, do this: if (!valobj.Equals(other.valobj)) returnfalse; returntrue; // Objects are equal. } // Optional overloads of the == and != operators publicstatic Boolean operator==(MyRefType o1, MyRefType o2) { if (o1 ==null) returnfalse; return o1.Equals(o2); } publicstatic Boolean operator!=(MyRefType o1, MyRefType o2) { return!(o1 == o2); } }
2)为基类没有重写Object.Equals方法的引用类型实现Equals方法
class MyRefType : BaseType { RefType refobj; // This field is a reference type. ValType valobj; // This field is a value type. publicoverride Boolean Equals(Object obj) { // Let the base type compare its fields. if (!base.Equals(obj)) returnfalse; // All the code from here down is identical to // that shown in the previous version. // Because ’this’ isn’t null, if obj is null, // then the objects can’t be equal. // NOTE: This line can be deleted if you trust that // the base type implemented Equals correctly. if (obj ==null) returnfalse; // If the objects are of different types, they can’t be equal. // NOTE: This line can be deleted if you trust that // the base type implemented Equals correctly. if (this.GetType() != obj.GetType()) returnfalse; // Cast obj to this type to access fields. NOTE: This cast // can’t fail because you know that objects are of the same type. MyRefType other = (MyRefType) obj; // To compare reference fields, do this: if (!Object.Equals(refobj, other.refobj)) returnfalse; // To compare value fields, do this: if (!valobj.Equals(other.valobj)) returnfalse; returntrue; // Objects are equal. } // Optional overloads of the == and != operators publicstatic Boolean operator==(MyRefType o1, MyRefType o2) { if (o1 ==null) returnfalse; return o1.Equals(o2); } publicstatic Boolean operator!=(MyRefType o1, MyRefType o2) { return!(o1 == o2); } }
class ValueType { publicoverride Boolean Equals(Object obj) { // Because ’this’ isn’t null, if obj is null, // then the objects can’t be equal. if (obj ==null) returnfalse; // Get the type of ’this’ object. Type thisType =this.GetType(); // If ’this’ and ’obj’ are different types, they can’t be equal. if (thisType != obj.GetType()) returnfalse; // Get the set of public and private instance // fields associated with this type. FieldInfo[] fields = thisType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Compare each instance field for equality. for (Int32 i =0; i < fields.Length; i++) { // Get the value of the field from both objects. Object thisValue = fields[i].GetValue(this); Object thatValue = fields[i].GetValue(obj); // If the values aren’t equal, the objects aren’t equal. if (!Object.Equals(thisValue, thatValue)) returnfalse; } // All the field values are equal, and the objects are equal. returntrue; } }
struct MyValType { RefType refobj; // This field is a reference type. ValType valobj; // This field is a value type. publicoverride Boolean Equals(Object obj) { // If obj is not your type, then the objects can’t be equal. if (!(obj is MyValType)) returnfalse; // Call the type-safe overload of Equals to do the work. returnthis.Equals((MyValType) obj); } // Implement a strongly typed version of Equals. public Boolean Equals(MyValType obj) { // To compare reference fields, do this: if (!Object.Equals(this.refobj, obj.refobj)) returnfalse; // To compare value fields, do this: if (!this.valobj.Equals(obj.valobj)) returnfalse; returntrue; // Objects are equal. } // Optionally overload operator== publicstatic Boolean operator==(MyValType v1, MyValType v2) { return (v1.Equals(v2)); } // Optionally overload operator!= publicstatic Boolean operator!=(MyValType v1, MyValType v2) { return!(v1 == v2); } }