Comparer Comparer Comparer Comparer Class

定義

比較兩個物件是否相等,其中字串比較是區分大小寫的。Compares two objects for equivalence, where string comparisons are case-sensitive.

public ref class Comparer sealed : System::Collections::IComparer, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
type Comparer = class
    interface IComparer
    interface ISerializable
Public NotInheritable Class Comparer
Implements IComparer, ISerializable
繼承
ComparerComparerComparerComparer
屬性
實作

範例

下列程式碼範例示範如何Compare會傳回不同的值,根據文化特性相關聯ComparerThe following code example shows how Compare returns different values depending on the culture associated with the Comparer.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
   
   // Creates the strings to compare.
   String^ str1 = "llegar";
   String^ str2 = "lugar";
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );
   
   // Uses the DefaultInvariant Comparer.
   Console::WriteLine( "   Invariant Comparer: {0}", Comparer::DefaultInvariant->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
   Comparer^ myCompIntl = gcnew Comparer( gcnew CultureInfo(  "es-ES",false ) );
   Console::WriteLine( "   International Sort: {0}", myCompIntl->Compare( str1, str2 ) );
   
   // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
   Comparer^ myCompTrad = gcnew Comparer( gcnew CultureInfo( 0x040A,false ) );
   Console::WriteLine( "   Traditional Sort  : {0}", myCompTrad->Compare( str1, str2 ) );
}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
using System;
using System.Collections;
using System.Globalization;


public class SamplesComparer  {

   public static void Main()  {

      // Creates the strings to compare.
      String str1 = "llegar";
      String str2 = "lugar";
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );

      // Uses the DefaultInvariant Comparer.
      Console.WriteLine( "   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) );
      Console.WriteLine( "   International Sort: {0}", myCompIntl.Compare( str1, str2 ) );

      // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) );
      Console.WriteLine( "   Traditional Sort  : {0}", myCompTrad.Compare( str1, str2 ) );

   }

}

/*
This code produces the following output.

Comparing "llegar" and "lugar" ...
   Invariant Comparer: -1
   International Sort: -1
   Traditional Sort  : 1

*/
Imports System
Imports System.Collections
Imports System.Globalization

Public Class SamplesComparer

   Public Shared Sub Main()

      ' Creates the strings to compare.
      Dim str1 As [String] = "llegar"
      Dim str2 As [String] = "lugar"
      Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2)

      ' Uses the DefaultInvariant Comparer.
      Console.WriteLine("   Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2))

      ' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
      Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False))
      Console.WriteLine("   International Sort: {0}", myCompIntl.Compare(str1, str2))

      ' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
      Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False))
      Console.WriteLine("   Traditional Sort  : {0}", myCompTrad.Compare(str1, str2))

   End Sub 'Main 

End Class 'SamplesComparer


'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
'   Invariant Comparer: -1
'   International Sort: -1
'   Traditional Sort  : 1

備註

這個類別是預設的實作IComparer介面。This class is the default implementation of the IComparer interface. CaseInsensitiveComparer類別是實作IComparer執行不區分大小寫的字串比較的介面。The CaseInsensitiveComparer class is the implementation of the IComparer interface that performs case-insensitive string comparisons. System.Collections.Generic.Comparer<T> 是這個類別的泛型對應項。System.Collections.Generic.Comparer<T> is the generic equivalent of this class.

比較程序使用Thread.CurrentCulture在目前的執行緒,除非另有指定。Comparison procedures use the Thread.CurrentCulture of the current thread unless otherwise specified. 字串比較可能會有不同的結果,根據文化特性。String comparisons might have different results depending on the culture. 如需有關特定文化特性比較的詳細資訊,請參閱 < System.Globalization 命名空間並全球化和當地語系化For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

建構函式

Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo)

使用指定的 Comparer,初始化 CultureInfo 類別的新執行個體。Initializes a new instance of the Comparer class using the specified CultureInfo.

欄位

Default Default Default Default

表示與目前執行緒的 Comparer 相關聯之 CurrentCulture 的執行個體。Represents an instance of Comparer that is associated with the CurrentCulture of the current thread. 此欄位為唯讀。This field is read-only.

DefaultInvariant DefaultInvariant DefaultInvariant DefaultInvariant

表示與 Comparer 相關聯的 InvariantCulture 之執行個體。Represents an instance of Comparer that is associated with InvariantCulture. 此欄位為唯讀。This field is read-only.

方法

Compare(Object, Object) Compare(Object, Object) Compare(Object, Object) Compare(Object, Object)

執行兩個相同型別物件的區分大小寫比較,並傳回表示是否某個物件小於、等於或大於另外一方的值。Performs a case-sensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

使用序列化 (Serialization) 所需的資料填入 SerializationInfo 物件。Populates a SerializationInfo object with the data required for serialization.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱