Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Someone can please give me any good reason to use IComparable interface

Discussion in 'Scripting' started by psykick1, Jul 7, 2020.

  1. psykick1


    May 19, 2020

    Someone can please give me any good reason to use IComparable interface and Implement CompareTo method , Instead of just declare CompareTo where and whenever I want without the interface?

    Thanks in Advance.
  2. lordofduct


    Oct 3, 2011

    Is part of the .net framework and has been since the beginning (which predates many modern patterns that have since been added).

    It's used by various built in classes such as SortedList which will sort based on the IComparable interface if no IComparer is supplied:

    Basically the default Comparer.Default and Comparer<T>.Default will both respect IComparable and IComparable<T> interfaces. Which is the way compared collections like SortedList, as well as sorting methods like Array.Sort and List.Sort work (they default to Comparer/<T>.Default if you don't give it an IComparer/<T> to use). It allows for a classic OOP pattern for comparing without things like delegates and the sort which early C# was modeled more around since it borrowed heavily from Java.

    Does this mean there is any use for it for you?

    No idea... that's up to you.

    As for:
    I'm not sure to what effect you mean.

    If you mean declare a comparison as a delegate via the Comparison delegate:

    Well yeah, that's the preferred method these days with C#.

    If you mean just arbitrarily inlined in code when you're just doing a basic comparison. Well then we're not talking about OOP patterns. And the benefits are numerous/debatable when it comes to using OOP patterns.


    But to give a basic example of when/why you might want to use the IComparable interface.

    Lets say you were creating a type similar to "System.String" which you expected to always sort in a very specific way whenever it was sorted... such as List.Sort or Array.Sort. And this class was something you distributed to a team, or to the public. It's beneficial to have that sorting behaviour built into the class rather than requiring for these 3rd parties to remember to pass in a special IComparer object or Comparison delegate. Where as "it just sorts correctly" when it has implemented IComparable/<T>.

    This is why System.String implements IComparable/<T>:


    Now whenever you treat string in a sorting/comparing situation it has a default behaviour that makes sense to it. Sorting alphabetically. Without any special extra lifting on your part.

    Now of course you likely wouldn't ever create your own custom string class as that's a bit weird. But less weird would be say if you created your own numeric type such as a custom BigInt, FixedPoint, or ComplexNumber class/struct.

    All types that you'd expect to implement IEquatable, IComparable, IConvertible as well as all the operators.
    Last edited: Jul 7, 2020