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 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 命名空間及 全球化與在地化。
建構函式
| 名稱 | Description |
|---|---|
| Comparer(CultureInfo) |
使用指定的 Comparer初始化類別的新實例CultureInfo。 |
欄位
| 名稱 | Description |
|---|---|
| Default |
代表與目前執行緒相關聯Comparer的實例CurrentCulture。 此欄位僅供讀取。 |
| DefaultInvariant |
代表與 相關聯Comparer的實例InvariantCulture。 此欄位僅供讀取。 |
方法
| 名稱 | Description |
|---|---|
| Compare(Object, Object) |
對兩個同類型物件進行大小寫區分比較,並回傳一個值,表示其中一個小於、等於或大於另一個。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
將序列化所需的資料填充物件 SerializationInfo 。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |