I’m writing an implementation of IComparable<T>.CompareTo(T) for a struct. I’m doing a member-wise comparison (i.e. d = a.CompareTo(other.a); if (d != 0) { return d; } etc.), but one of the members is of a class (let’s call it Y) that doesn’t implement IComparable or have any other reasonable way of comparing so I just want to compare the references (in this case I know that all instances of Y are unique). Is there a way of doing that that will yield an int that is suitable for use in a comparison method?
I’m writing an implementation of IComparable<T>.CompareTo(T) for a struct. I’m doing a member-wise comparison
Share
Comparing the results of
Object.GetHashCode()works well enough for my purposes (I just care about reference equality and it doesn’t matter where anything unequal is sorted. I want instances ofTthat have the same instances ofYas members to be next to each other in the sorted result). MyT.CompareTo()uses the following helper: