Comparer<T>.IComparer.Compare(Object, Object) 메서드

정의

두 개체를 비교하여 한 개체가 다른 개체보다 작거나, 같거나 또는 크다는 것을 나타내는 값을 반환합니다.

 virtual int System.Collections.IComparer.Compare(System::Object ^ x, System::Object ^ y) = System::Collections::IComparer::Compare;
int IComparer.Compare (object x, object y);
abstract member System.Collections.IComparer.Compare : obj * obj -> int
override this.System.Collections.IComparer.Compare : obj * obj -> int
Function Compare (x As Object, y As Object) As Integer Implements IComparer.Compare

매개 변수

x
Object

비교할 첫 번째 개체입니다.

y
Object

비교할 두 번째 개체입니다.

반환

다음 표와 같이 xy의 상대 값을 나타내는 부호 있는 정수입니다.

의미
0보다 작음xy보다 작은 경우
0xy와 같습니다.
0보다 큼xy보다 큰 경우

구현

예외

x 또는 yT 형식으로 캐스팅할 수 없는 형식인 경우

또는

xyIComparable<T> 제네릭 인터페이스나 IComparable 인터페이스를 구현하지 않는 경우

예제

다음 예제에서는 메서드를 사용하여 두 개체를 IComparer.Compare 비교하는 방법을 보여줍니다. 이 예제는에 대해 제공 된 큰 예제의 일부는 Comparer<T> 클래스입니다.


// This explicit interface implementation
// compares first by the length.
// Returns -1 because the length of BoxA
// is less than the length of BoxB.
BoxLengthFirst LengthFirst = new BoxLengthFirst();

Comparer<Box> bc = (Comparer<Box>) LengthFirst;

Box BoxA = new Box(2, 6, 8);
Box BoxB = new Box(10, 12, 14);
int x = LengthFirst.Compare(BoxA, BoxB);
Console.WriteLine();
Console.WriteLine(x.ToString());

' This explicit interface implementation
' compares first by the length.
' Returns -1 because the length of BoxA
' is less than the length of BoxB.
Dim LengthFirst As New BoxLengthFirst()

Dim bc As Comparer(Of Box) = CType(LengthFirst, Comparer(Of Box))

Dim BoxA As New Box(2, 6, 8)
Dim BoxB As New Box(10, 12, 14)
Dim x As Integer = LengthFirst.Compare(BoxA, BoxB)
Console.WriteLine()
Console.WriteLine(x.ToString())

설명

이 메서드는 메서드의 Compare(T, T) 래퍼이므로 obj 현재 instance 제네릭 인수 T 로 지정된 형식으로 캐스팅해야 합니다. 로 T캐스팅할 수 없는 경우 가 ArgumentException throw됩니다.

null 모든 참조 형식과 비교할 수 있으며 예외를 생성하지 않습니다. 정렬할 null 때 는 다른 개체보다 작은 것으로 간주됩니다.

호출자 참고

Compare(T, T) 문화 Equals(T, T) 권 민감도 및 대/소문자 구분 측면에서 다르게 동작합니다.

문자열 비교의 경우 클래스는 보다 StringComparerComparer<String>권장됩니다. 클래스의 속성은 StringComparer 문화권 민감도 및 대/소문자 구분의 다양한 조합으로 문자열 비교를 수행하는 미리 정의된 인스턴스를 반환합니다. 대/소문자 구분 및 문화권 민감도는 동일한 StringComparer instance 멤버 간에 일치합니다.

문화권별 비교에 대한 자세한 내용은 네임스페이 System.Globalization 스 및 세계화 및 지역화를 참조하세요.

적용 대상

추가 정보