Question

I really hate working with IComparer - to this day, after years of working with .Net, I still get regularly confused by those 1s and -1s.

Can I somehow replace the Compare result values with some self-explanatory names without having to cast the output integers to something else after every Compare call?

I tried to define an enum like this:

public enum ComparerResult
{
    ALessThanB = -1,
    Equal = 0,
    AGreaterThanB = 1
}

if(comparer.Compare(a, b) == ComparerResult.ALessThanB)

But that of course won't compile without a cast.

This of course also applies to IComparable.CompareTo.

Thanks for your ideas

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top