How to compare struct
C#
4
Posts
4
Posters
0
Views
1
Watching
-
firstStruct.Equals(secondStruct)
-
I guess that depends on how you're going to use them and if the structs are the same type. The IComparer interface is what you'd want to use if you didn't have control of the structs and you were trying to sort them in a list (for example). You could also implement the IComparable interface on the struct if you have control over them.
-
You should not use Equals if you want to compare for true equality, because Equals only checks if the compared values refer to the same instance. To determine true equality of value you have to compare the fields. See Richter for further information. The base implementation actually compares for identity, not equality.