Supports the structural comparison of collection objects.

See Also: IStructuralComparable Members


public interface IStructuralComparable


The IStructuralComparable interface enables you to implement customized comparisons for collection members. That is, you can define precisely what it means for one collection object to precede, follow, or occur in the same position in the sort order as a second collection object. You can then specify that this definition be used with a collection type that accepts the IStructuralComparable interface.

The interface has a single member, IStructuralComparable.CompareTo(object, IComparer), which determines whether the current collection object is less than, equal to, or greater than a second object in the sort order. The actual comparison of the members or elements in the current instance with those in a second object is performed by an IComparer interface implementation, which contains the definition of your custom comparison.


The IStructuralComparable interface supports only structural comparisons for sorting or ordering. The IStructuralEquatable interface supports custom comparisons for structural equality.

The .NET Framework provides two default comparers. One is returned by the StructuralComparisons.StructuralComparer property; the other is returned by the Comparer`1.Default property.

The generic tuple classes (Tuple`1, Tuple`2, Tuple`3, and so on) and the Array class provide explicit implementations of the IStructuralComparable interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralComparable interface value and providing your IComparer implementation as an argument to the IStructuralComparable.CompareTo(object, IComparer) method, you can define a custom sort order for the array or collection. However, you do not call the IStructuralComparable.CompareTo(object, IComparer) method directly in most cases. Instead, the IStructuralComparable.CompareTo(object, IComparer) method is called by sorting methods such as Array.Sort(Array, IComparer). In this case, you define your IComparer implementation and pass it as an argument to a sorting method or collection object's class constructor. The IStructuralComparable.CompareTo(object, IComparer) method with your custom comparer is then called automatically whenever the collection is sorted.


Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: