If we want to create a class, whether it is a reference or a value type, we often need to implement the equals method. The following two general implementation methods are written here
1. Implementation of the reference type
Class MyRefType: BaseType {
RefType refobj; // reference type object
ValType valobj; // value type object
Public override bool Equals (object obj)
{
// When the base class implements equals, first call the equals method of the base class and then perform other operations. If the base class does not override the equals method, this judgment can be removed.
If (! Base. Equals (obj) return false;
// Because this cannot be null, if obj is null, it cannot be equal.
If (obj = null ){
Return false;
}
// If the two objects have different types, they cannot be equal.
If (this. GetType ()! = Obj. GetType () return false;
//
MyRefType other = (MyRefType) obj;
If (! Object. equals (refobj, other. refobj) return false; // any type of variable in the reference type can be determined using the static equals method of the object, however, the equals method of the value type class is different here and can be used for reference, but the value type variable changes.
If (! Object. Equals (valobj, other. valobj) return false;
Return true;
}
// When the equals method is rewritten, it must be "=" and "! = "To rewrite
Public static Boolean operator = (MyRefType o1, MyRefType o2 ){
Return object. Equals (o1, o2 );
}
Public static Boolean operator! = (MyRefType o1, MyRefType o2 ){
Return