System.IEquatable

Introduced in .NET 2.0
This interface is for an object to compare itself to another object.


Which Interface ?

IEquatable - Provides an equality check when there is only one way of comparing the objects (implemented inside the class)
IEqualityComparer - Allows you to define and use multiple equality checks (implemented outside the class)


This protects both the caller and callee from passing incompatible object types. It also avoids the overhead of boxing for value types.
Defines a type safe method for creating a type-specific method to determine equality of instances
This interface is used by generic collection objects such as Dictionary <TKey, TValue> and List <T> when testing for equality in the following methods: Contains, IndexOf, LastIndexOf, Remove.
This interface should be implemented by an object that might be stored in a generic collection.


public class SecurityClass : System.IEquatable<SecurityClass> 
    {
        public int? SecurityID { get; set; }

        //Instance Method - IEquatable.Equals
        public bool Equals(SecurityClass other)
        {
            if (other == null)
            {
                return false;
            }
            if (this.SecurityID == other.SecurityID)
            {
                return true;
            }
            else
            {
                return false;
            }
            //else { return System.Object.ReferenceEquals(this, other); }
        }

        //Overridding - System.Object.Equals
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
            {
                return false;
            }

            SecurityClass securityclassObj = obj as SecurityClass;
            if (securityclassObj == null)
            {
                return false;
            }
            else
            {
                return this.Equals(securityclassObj);
            }
        }

        //Overridding - System.Object.GetHashCode
        public override int GetHashCode()
        {
            if (this.SecurityID == null)
            {
                return 0;
            }
            else
            {
                return this.SecurityID.GetHashCode();
            }
        }

        //Overloading - Equality
        public static bool operator ==(SecurityClass security1, SecurityClass security2)
        {
            if ((object)security1 == null || ((object)security2) == null)
            {
                return System.Object.Equals(security1, security2);
            }

            return security1.Equals(security2);
        }

        //Overloading - InEquality
        public static bool operator !=(SecurityClass security1, SecurityClass security2)
        {
            return !(security1 == security2);
        }
    }


Equals Method

This interface defines the Equals method which is used to determine the equaity of the implementing type.
Indicates if the current object is equal to another object of the same type


GetHashCode Method

The GetHashCode method generates a number based on the value of every property in the object.
The default implementation relies on IEquatable for its implementation
The default implementation use object.GetHashCode
Therefore in order for IEqualityComparer to work correctly the object must implement GetHashCode


.NET 1.1

It was possible to compare for equality by overriding the virtual object Equals method

public override bool Equals(object obj) 
{
   var other obj As MyObject
   if (other == null) { return false; }
   //etc
}

This method is loosely typed because it takes an object as the parameter


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

PrevNext