# 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

#### 返回

`x` 等于 `y`

#### 例外

`x``y` 所属的类型无法被强制转换为类型 `T`

- 或 -

`x``y` 不实现 IComparable<T> 泛型接口或 IComparable 接口。

## 示例

``````
// 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())
``````

## 注解

`null`允许与任何引用类型进行比较，并且不会生成异常。 排序时， `null` 被视为小于任何其他对象。

## 调用方说明

Compare(T, T)Equals(T, T) 的行为在区域性敏感度和区分大小写方面有所不同。