Permalink
Fetching contributors…
Cannot retrieve contributors at this time
150 lines (125 sloc) 8.16 KB
<Type Name="IComparer&lt;T&gt;" FullName="System.Collections.Generic.IComparer&lt;T&gt;">
<TypeSignature Language="C#" Value="public interface IComparer&lt;in T&gt;" />
<TypeSignature Language="ILAsm" Value=".class public interface auto ansi abstract IComparer`1&lt;- T&gt;" />
<TypeSignature Language="DocId" Value="T:System.Collections.Generic.IComparer`1" />
<TypeSignature Language="VB.NET" Value="Public Interface IComparer(Of In T)" />
<TypeSignature Language="C++ CLI" Value="generic &lt;typename T&gt;&#xA;public interface class IComparer" />
<TypeSignature Language="F#" Value="type IComparer&lt;'T&gt; = interface" />
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeParameters>
<TypeParameter Name="T">
<Constraints>
<ParameterAttribute>Contravariant</ParameterAttribute>
</Constraints>
</TypeParameter>
</TypeParameters>
<Interfaces />
<Docs>
<typeparam name="T">The type of objects to compare.</typeparam>
<summary>Defines a method that a type implements to compare two objects.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This interface is used with the <xref:System.Collections.Generic.List%601.Sort%2A?displayProperty=nameWithType> and <xref:System.Collections.Generic.List%601.BinarySearch%2A?displayProperty=nameWithType> methods. It provides a way to customize the sort order of a collection. Classes that implement this interface include the <xref:System.Collections.Generic.SortedDictionary%602> and <xref:System.Collections.Generic.SortedList%602> generic classes.
The default implementation of this interface is the <xref:System.Collections.Generic.Comparer%601> class. The <xref:System.StringComparer> class implements this interface for type <xref:System.String>.
This interface supports ordering comparisons. That is, when the <xref:System.Collections.Generic.Comparer%601.Compare%2A> method returns 0, it means that two objects sort the same. Implementation of exact equality comparisons is provided by the <xref:System.Collections.Generic.IEqualityComparer%601> generic interface.
We recommend that you derive from the <xref:System.Collections.Generic.Comparer%601> class instead of implementing the <xref:System.Collections.Generic.IComparer%601> interface, because the <xref:System.Collections.Generic.Comparer%601> class provides an explicit interface implementation of the <xref:System.Collections.Generic.Comparer%601.System%23Collections%23IComparer%23Compare%2A> method and the <xref:System.Collections.Generic.Comparer%601.Default%2A> property that gets the default comparer for the object.
## Examples
The following example implements the <xref:System.Collections.Generic.IComparer%601> interface to compare objects of type `Box` according to their dimensions. This example is part of a larger example provided for the <xref:System.Collections.Generic.Comparer%601> class.
[!code-csharp[System.Collections.Generic.BoxCompare#7](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.collections.generic.boxcompare/cs/program.cs#7)]
[!code-vb[System.Collections.Generic.BoxCompare#7](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.collections.generic.boxcompare/vb/program.vb#7)]
]]></format>
</remarks>
<altmember cref="T:System.Collections.Generic.Comparer`1" />
<altmember cref="N:System.Collections" />
<altmember cref="P:System.Threading.Thread.CurrentCulture" />
<altmember cref="T:System.Globalization.CompareInfo" />
<altmember cref="T:System.Globalization.CultureInfo" />
<altmember cref="T:System.Collections.Generic.IEqualityComparer`1" />
</Docs>
<Members>
<Member MemberName="Compare">
<MemberSignature Language="C#" Value="public int Compare (T x, T y);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance int32 Compare(!T x, !T y) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Collections.Generic.IComparer`1.Compare(`0,`0)" />
<MemberSignature Language="VB.NET" Value="Public Function Compare (x As T, y As T) As Integer" />
<MemberSignature Language="F#" Value="abstract member Compare : 'T * 'T -&gt; int" Usage="iComparer.Compare (x, y)" />
<MemberSignature Language="C++ CLI" Value="public:&#xA; int Compare(T x, T y);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Runtime</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.0.20.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
<AssemblyVersion>4.2.1.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="x" Type="T" />
<Parameter Name="y" Type="T" />
</Parameters>
<Docs>
<param name="x">The first object to compare.</param>
<param name="y">The second object to compare.</param>
<summary>Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.</summary>
<returns>A signed integer that indicates the relative values of <paramref name="x" /> and <paramref name="y" />, as shown in the following table.
<list type="table"><listheader><term> Value
</term><description> Meaning
</description></listheader><item><term> Less than zero
</term><description><paramref name="x" /> is less than <paramref name="y" />.
</description></item><item><term> Zero
</term><description><paramref name="x" /> equals <paramref name="y" />.
</description></item><item><term> Greater than zero
</term><description><paramref name="x" /> is greater than <paramref name="y" />.
</description></item></list></returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Implement this method to provide a customized sort order comparison for type `T`.
Comparing `null` with any reference type is allowed and does not generate an exception. A null reference is considered to be less than any reference that is not null.
## Examples
The following example implements the <xref:System.Collections.Generic.IComparer%601> interface to compare objects of type `Box` according to their dimensions. This example is part of a larger example provided for the <xref:System.Collections.Generic.Comparer%601> class.
[!code-csharp[System.Collections.Generic.BoxCompare#7](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.collections.generic.boxcompare/cs/program.cs#7)]
[!code-vb[System.Collections.Generic.BoxCompare#7](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.collections.generic.boxcompare/vb/program.vb#7)]
]]></format>
</remarks>
<altmember cref="T:System.IComparable`1" />
<altmember cref="P:System.Threading.Thread.CurrentCulture" />
<altmember cref="T:System.Globalization.CompareInfo" />
<altmember cref="T:System.Globalization.CultureInfo" />
</Docs>
</Member>
</Members>
</Type>