Represents the method that compares two objects of the same type.
Assemblies: System.Runtime (in System.Runtime.dll)
mscorlib (in mscorlib.dll)
- in T
The type of the objects to compare.
This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.
Thetype exposes the following members.
|DynamicInvoke||Dynamically invokes (late-bound) the method represented by the current delegate. (Inherited from Delegate.)|
|Equals||Determines whether this multicast delegate and the specified object are equal. (Inherited from MulticastDelegate.)|
|GetHashCode||Returns the hash code for this instance. (Inherited from MulticastDelegate.)|
|GetInvocationList||Returns the invocation list of this multicast delegate, in invocation order. (Inherited from MulticastDelegate.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
The following code example demonstrates the use of the delegate with the Sort(Comparison(T)) method overload.
The code example defines an alternative comparison method for strings, named CompareDinosByLength. This method works as follows: First, the comparands are tested for a null reference (Nothing in Visual Basic), and a null reference is treated as less than a non-null. Second, the string lengths are compared, and the longer string is deemed to be greater. Third, if the lengths are equal, ordinary string comparison is used.
A List(T) of strings is created and populated with four strings, in no particular order. The list also includes an empty string and a null reference. The list is displayed, sorted using a generic delegate representing the CompareDinosByLength method, and displayed again.
The following example uses the delegate to sort the elements of a collection of CityInfo objects. CityInfo is an application-defined class that contains information about a city and its population. The example defines three methods, CompareByName, CompareByPopulation, and CompareByNames, that offer three different ways of ordering the CityInfo objects. Each method is assigned to the comparison argument of the Array.Sort(T)(T, Comparison(T)) method.
.NET FrameworkSupported in: 4.5.3, 4, 3.5, 3.0, 2.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.