Can be used to determine equality of class instances.
1 | var obj1 = new Foo(); |
Object State Equality
Consider Foo
has the following properties, for the above these would have received the default values right? So 0 and Null, so why did the quality fail? We already know it was the memory reference not being the same as custom types like Foo
are reference types.
1 | public class Foo |
You can fix this by simply inheriting and implementing IEquatable<T>
where T
is the type you want to compare with.
1 | public class Foo : IEquatable<Foo> |
So now equals has been overridden to compare the state.
1 | var obj1 = new Foo() |