# Comparer<T>.IComparer.Compare(Object, Object)Comparer<T>.IComparer.Compare(Object, Object)Comparer<T>.IComparer.Compare(Object, Object) Method

## 定義

`` virtual int System.Collections.IComparer.Compare(System::Object ^ x, System::Object ^ y) = System::Collections::IComparer::Compare;``
``int IComparer.Compare (object x, object y);``
``Function Compare (x As Object, y As Object) As Integer Implements IComparer.Compare``

#### 參數

x
Object Object Object

y
Object Object Object

#### 傳回

Value 意義Meaning

Zero `x`等於 `y``x` equals `y`.

#### 例外狀況

`x``y` 的型別無法轉型為型別 `T``x` or `y` is of a type that cannot be cast to type `T`.

-或--or- `x``y` 不實作 IComparable<T> 泛型介面或 IComparable 介面。`x` and `y` do not implement either the IComparable<T> generic interface or the IComparable interface.

## 範例

``````
// 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)System.Collections.Generic.EqualityComparer`1.Equals(\`0,\`0)區分文化特性和區分大小寫方面有不同的行為。 Compare(T, T) and System.Collections.Generic.EqualityComparer`1.Equals(\`0,\`0) behave differently in terms of culture-sensitivity and case-sensitivity.