I am trying to compare two structs using equals (==) in C#. My struct is below:
public struct CisSettings : IEquatable
{
public int Ga
you must overload "==" operator, but also overload "!=" operator. (Look at this Note)
For overloading operator, see this page
When you override the .Equals method, the == operator isn't automatically overloaded. You need to do that explicitly.
See also Guidelines for Overriding Equals() and Operator ==.
You should overload your operator is some way like this:
public static bool operator ==(CisSettings a, CisSettings b)
{
return a.Equals(b);
}
You need to overload the == and != operators. Add this to your struct:
public static bool operator ==(CisSettings c1, CisSettings c2)
{
return c1.Equals(c2);
}
public static bool operator !=(CisSettings c1, CisSettings c2)
{
return !c1.Equals(c2);
}
You don't implement explicitly an equality operator, so == is not defined particularly for the type.
You need to override operator == explicitly.
public static bool operator ==(CisSettings x, CisSettings y)
{
return x.Equals(y);
}
By the way, you'd better put the comparing code in public bool Equals(CisSettings other), and let bool Equals(object obj) call bool Equals(CisSettings other), so that you can gain some performance by avoiding unnecessary type check.