Given the class as follows,
public class Number
{
public int X { get; set; }
public int Y { get; set; }
}
How to define an overload operato
overload the operator yourself:
public static bool operator ==(Number a, Number b)
{
// do stuff
return true/false;
}
Here is a short example of how to do it, but I very strongly recommend reading Guidelines for Overloading Equals() and Operator == (C# Programming Guide) to understand the interaction between Equals() and == and so on.
public class Number
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Number a, Number b)
{
// TODO
}
}
From the MSDN Docs:
public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
{
return !(a == b);
}
public static bool operator ==(YourClassType a, YourClassType b)
{
// do the comparison
}
More information here. In short: