IEquatable

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


The System.IEquatable interface defines a type-safe method for creating a type-specific method to determine equality of instances.

Note: This interface is new in the .NET Framework version 2.0.


Syntax

Declaration syntax

public interface IEquatable<T>

Method syntax

The Equals method indicates whether the current object is equal to another object of the same type.

bool Equals ( T other )

where other is an object of the same type to be compared with this object.

Returns true if the current object is equal to the parameter object; otherwise, false.


Example

Vector myVA = new Vector(1.5, 1.5, 1.5);
Vector myVB = new Vector(1.5, 1.5, 1.5);
 
if (myVA == myVB)
{
   Console.WriteLine("myVA = myVB");
}
else
{
   Console.WriteLine("myVA != myVB");
}
 
if (myVA.Equals(myVB))
{
   Console.WriteLine("myVA = myVB");
}
else
{
   Console.WriteLine("myVA != myVB");
}


class Vector: IEquatable<Vector>
  {
    private double m_X;
    private double m_Y;
    private double m_Z;
 
    public Vector(double x, double y, double z)
    {
      m_X = x;
      m_Y = y;
      m_Z = z;
    }
 
    public double getX()
    {
      return m_X;
    }
 
    public double getY()
    {
      return m_Y;
    }
 
    public double getZ()
    {
      return m_Z;
    }
 
    #region IEquatable<Vector> Members
 
    public bool Equals(Vector other)
    {
      bool result = false;
 
      if (this.m_X.Equals(other.getX()) &&
            this.m_Y.Equals(other.getY()) && this.m_Z.Equals(other.getZ()))
      {
        result = true;
      }
 
      return result;
    }
 
    #endregion
  }
Result

myVA != myVB
myVA = myVB

The reason for the first result is because the == compares the value stored in the variables myVA and myVB. Because these represent instances of classes they are reference types, so the values are memory addresses of where the actual object is stored in the heap. Therefore they can never be identical unless they actually point to the same object.

The second result is from the Equals() method, this returns the correct result as the class has compared the actual values.


IEquatable.Equals vs. System.Object.Equals

The IEquatable.Equals method is very similar to Object.Equals. However, IEquatable.Equals is type-safe and generic—requiring no boxing and unboxing.


IEquatable vs. IComparable

At first glance, the IEquatable interface appears very similar to the IComparable interface. But, whereas IEquatable returns a bool representing only whether the instances are equal or not, IComparable returns an int indicating how the instances differ—whether smaller, equal or larger.


MSDN references

IEquatable Generic Interface


Personal tools