I have a class like this
public class TestData
{
public string Name {get;set;}
public string type {get;set;}
public List Members = ne
There are three ways objects of some reference type T can be compared to each other:
IEquatable.Equals (only for types that implement IEquatable)==Furthermore, there are two possibilities for each of these cases:
T (or some other base of T)objectThe rules you absolutely need to know are:
Equals and operator== is to test for reference equalityEquals will work correctly no matter what the static type of the objects being compared isIEquatable.Equals should always behave the same as object.Equals, but if the static type of the objects is T it will offer slightly better performanceSo what does all of this mean in practice?
As a rule of thumb you should use Equals to check for equality (overriding object.Equals as necessary) and implement IEquatable as well to provide slightly better performance. In this case object.Equals should be implemented in terms of IEquatable.
For some specific types (such as System.String) it's also acceptable to use operator==, although you have to be careful not to make "polymorphic comparisons". The Equals methods, on the other hand, will work correctly even if you do make such comparisons.
You can see an example of polymorphic comparison and why it can be a problem here.
Finally, never forget that if you override object.Equals you must also override object.GetHashCode accordingly.