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

定義

比較兩個物件並傳回值,指出其中一個物件為小於、等於或大於另一個物件。Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

 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

要比較的第一個物件。The first object to compare.

y
Object Object Object

要比較的第二個物件。The second object to compare.

傳回

帶正負號的整數,表示 xy 的相對值,如下表所示。A signed integer that indicates the relative values of x and y, as shown in the following table.

Value 意義Meaning
小於零Less than zero x 小於 yx is less than y.
Zero x等於 yx equals y.
大於零Greater than zero x 大於 yx is greater than y.

實作

例外狀況

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

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

範例

下列範例示範如何使用IComparer.Compare方法來比較兩個物件。The following example shows how to use the IComparer.Compare method to compare two objects. 這個範例是針對提供之較大範例的一部分Comparer<T>類別。This example is part of a larger example provided for the Comparer<T> class.


// 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必須轉換成泛型引數所指定的型別T目前執行個體。This method is a wrapper for the Compare(T, T) method, so obj must be cast to the type specified by the generic argument T of the current instance. 如果無法轉換成TArgumentException就會擲回。If it cannot be cast to T, an ArgumentException is thrown.

比較null具有任何參考型別允許和不會產生例外狀況。Comparing null with any reference type is allowed and does not generate an exception. 排序時,null會被視為小於任何其他物件。When sorting, null is considered to be less than any other object.

給呼叫者的注意事項

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.

字串比較StringComparer建議類別比較子<字串>For string comparisons, the StringComparer class is recommended over Comparer<String>. 屬性的StringComparer類別傳回預先定義的執行個體執行的不同組合的區分文化特性和區分大小寫的字串比較。Properties of the StringComparer class return predefined instances that perform string comparisons with different combinations of culture-sensitivity and case-sensitivity. 區分大小寫和區分文化特性都是一致的相同成員之間StringComparer執行個體。The case-sensitivity and culture-sensitivity are consistent among the members of the same StringComparer instance.

如需有關特定文化特性比較的詳細資訊,請參閱 <<c0> System.Globalization 命名空間並全球化和當地語系化For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

適用於

另請參閱