Comparer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
比較兩個物件是否相等,其中字串比較是區分大小寫的。
public ref class Comparer sealed : System::Collections::IComparer
public ref class Comparer sealed : System::Collections::IComparer, System::Runtime::Serialization::ISerializable
public sealed class Comparer : System.Collections.IComparer
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
[System.Serializable]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
type Comparer = class
interface IComparer
type Comparer = class
interface IComparer
interface ISerializable
[<System.Serializable>]
type Comparer = class
interface IComparer
interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Comparer = class
interface IComparer
interface ISerializable
Public NotInheritable Class Comparer
Implements IComparer
Public NotInheritable Class Comparer
Implements IComparer, ISerializable
- 繼承
-
Comparer
- 屬性
- 實作
範例
下列程式代碼範例示範如何 Compare 根據與 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.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
End Class
'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
' Invariant Comparer: -1
' International Sort: -1
' Traditional Sort : 1
備註
這個類別是介面的預設實作 IComparer 。 類別 CaseInsensitiveComparer 是介面的 IComparer 實作,可執行不區分大小寫的字串比較。 System.Collections.Generic.Comparer<T> 是這個類別的泛型對等專案。
除非另有指定,否則比較程式會使用 Thread.CurrentCulture 目前線程的 。 根據文化特性,字串比較可能會有不同的結果。 如需特定文化特性比較的詳細資訊,請參閱 System.Globalization 命名空間和 全球化和當地語系化。
建構函式
Comparer(CultureInfo) |
使用指定的 Comparer,初始化 CultureInfo 類別的新執行個體。 |
欄位
Default |
表示與目前執行緒的 Comparer 相關聯之 CurrentCulture 的執行個體。 此欄位為唯讀。 |
DefaultInvariant |
表示與 Comparer 相關聯的 InvariantCulture 之執行個體。 此欄位為唯讀。 |
方法
Compare(Object, Object) |
執行兩個相同型別物件的區分大小寫比較,並傳回表示是否某個物件小於、等於或大於另外一方的值。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
使用序列化 (Serialization) 所需的資料填入 SerializationInfo 物件。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |