IComparer<T> 인터페이스
정의
형식에서 두 개체를 비교하기 위해 구현하는 메서드를 정의합니다.Defines a method that a type implements to compare two objects.
generic <typename T>
public interface class IComparer
public interface IComparer<in T>
public interface IComparer<T>
type IComparer<'T> = interface
Public Interface IComparer(Of In T)
Public Interface IComparer(Of T)
형식 매개 변수
- T
비교할 개체의 형식입니다.The type of objects to compare.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- 파생
예제
다음 예제에서는 인터페이스를 구현 IComparer<T> 하 여 Box
해당 차원에 따라 형식의 개체를 비교 합니다.The following example implements the IComparer<T> interface to compare objects of type Box
according to their dimensions. 이 예제는에 대해 제공 된 큰 예제의 일부는 Comparer<T> 클래스입니다.This example is part of a larger example provided for the Comparer<T> class.
// This class is not demonstrated in the Main method
// and is provided only to show how to implement
// the interface. It is recommended to derive
// from Comparer<T> instead of implementing IComparer<T>.
public class BoxComp : IComparer<Box>
{
// Compares by Height, Length, and Width.
public int Compare(Box x, Box y)
{
if (x.Height.CompareTo(y.Height) != 0)
{
return x.Height.CompareTo(y.Height);
}
else if (x.Length.CompareTo(y.Length) != 0)
{
return x.Length.CompareTo(y.Length);
}
else if (x.Width.CompareTo(y.Width) != 0)
{
return x.Width.CompareTo(y.Width);
}
else
{
return 0;
}
}
}
' This class is not demonstrated in the Main method
' and is provided only to show how to implement
' the interface. It is recommended to derive
' from Comparer<T> instead of implementing IComparer<T>.
Public Class BoxComp
Implements IComparer(Of Box)
' Compares by Height, Length, and Width.
Public Function Compare(ByVal x As Box, ByVal y As Box) As Integer Implements _
IComparer(Of Box).Compare
If x.Height.CompareTo(y.Height) <> 0 Then
Return x.Height.CompareTo(y.Height)
ElseIf x.Length.CompareTo(y.Length) <> 0 Then
Return x.Length.CompareTo(y.Length)
ElseIf x.Width.CompareTo(y.Width) <> 0 Then
Return x.Width.CompareTo(y.Width)
Else
Return 0
End If
End Function
End Class
설명
이 인터페이스는 및 메서드와 함께 사용 됩니다 List<T>.Sort List<T>.BinarySearch .This interface is used with the List<T>.Sort and List<T>.BinarySearch methods. 컬렉션의 정렬 순서를 사용자 지정 하는 방법을 제공 합니다.It provides a way to customize the sort order of a collection. 이 인터페이스를 구현 하는 클래스에는 SortedDictionary<TKey,TValue> 및 SortedList<TKey,TValue> 제네릭 클래스가 포함 됩니다.Classes that implement this interface include the SortedDictionary<TKey,TValue> and SortedList<TKey,TValue> generic classes.
이 인터페이스의 기본 구현은 Comparer<T> 클래스입니다.The default implementation of this interface is the Comparer<T> class. StringComparer클래스는 형식에 대해이 인터페이스를 구현 합니다 String .The StringComparer class implements this interface for type String.
이 인터페이스는 순서 비교를 지원 합니다.This interface supports ordering comparisons. 즉, Compare 메서드가 0을 반환 하는 경우 두 개체가 동일한를 정렬 하는 것을 의미 합니다.That is, when the Compare method returns 0, it means that two objects sort the same. 제네릭 인터페이스는 정확히 같음 비교의 구현을 제공 IEqualityComparer<T> 합니다.Implementation of exact equality comparisons is provided by the IEqualityComparer<T> generic interface.
인터페이스를 구현 하는 대신 클래스에서 파생 하는 것이 좋습니다 Comparer<T> IComparer<T> Comparer<T> . 클래스는 메서드의 명시적 인터페이스 구현을 제공 하 IComparer.Compare 고 Default 개체에 대 한 기본 비교자를 가져오는 속성을 제공 하기 때문입니다.We recommend that you derive from the Comparer<T> class instead of implementing the IComparer<T> interface, because the Comparer<T> class provides an explicit interface implementation of the IComparer.Compare method and the Default property that gets the default comparer for the object.
메서드
Compare(T, T) |
두 개체를 비교하여 한 개체가 다른 개체보다 작거나, 같거나 또는 크다는 것을 나타내는 값을 반환합니다.Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other. |