語言

CaseInsensitiveComparer 類別

定義

比較兩個物件以求等價,忽略弦的情況。

public ref class CaseInsensitiveComparer : System::Collections::IComparer
public class CaseInsensitiveComparer : System.Collections.IComparer
[System.Serializable]
public class CaseInsensitiveComparer : System.Collections.IComparer
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CaseInsensitiveComparer : System.Collections.IComparer
type CaseInsensitiveComparer = class
    interface IComparer
[<System.Serializable>]
type CaseInsensitiveComparer = class
    interface IComparer
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CaseInsensitiveComparer = class
    interface IComparer
Public Class CaseInsensitiveComparer
Implements IComparer
繼承
CaseInsensitiveComparer
屬性
實作

範例

以下程式碼範例建立了一個大小寫區分的雜湊表與一個不區分大小寫的雜湊表,並展示即使兩者包含相同元素,行為上的差異。

using System;
using System.Collections;
using System.Globalization;

public class SamplesHashtable  {

   public static void Main()  {

      // Create a Hashtable using the default hash code provider and the default comparer.
      Hashtable myHT1 = new Hashtable();
      myHT1.Add("FIRST", "Hello");
      myHT1.Add("SECOND", "World");
      myHT1.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the culture of the current thread.
      Hashtable myHT2 = new Hashtable( new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() );
      myHT2.Add("FIRST", "Hello");
      myHT2.Add("SECOND", "World");
      myHT2.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the InvariantCulture.
      Hashtable myHT3 = new Hashtable( CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant );
      myHT3.Add("FIRST", "Hello");
      myHT3.Add("SECOND", "World");
      myHT3.Add("THIRD", "!");

      // Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      // based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
      CultureInfo myCul = new CultureInfo( "tr-TR" );
      Hashtable myHT4 = new Hashtable( new CaseInsensitiveHashCodeProvider( myCul ), new CaseInsensitiveComparer( myCul ) );
      myHT4.Add("FIRST", "Hello");
      myHT4.Add("SECOND", "World");
      myHT4.Add("THIRD", "!");

      // Search for a key in each hashtable.
      Console.WriteLine( "first is in myHT1: {0}", myHT1.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT2: {0}", myHT2.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT3: {0}", myHT3.ContainsKey( "first" ) );
      Console.WriteLine( "first is in myHT4: {0}", myHT4.ContainsKey( "first" ) );
   }
}


/*
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: True
first is in myHT4: False

*/
Imports System.Collections
Imports System.Globalization

Public Class SamplesHashtable

   Public Shared Sub Main()

      ' Create a Hashtable using the default hash code provider and the default comparer.
      Dim myHT1 As New Hashtable()
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the culture of the current thread.
      Dim myHT2 As New Hashtable(New CaseInsensitiveHashCodeProvider(), New CaseInsensitiveComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the InvariantCulture.
      Dim myHT3 As New Hashtable(CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant)
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Create a Hashtable using a case-insensitive code provider and a case-insensitive comparer,
      ' based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT4 As New Hashtable(New CaseInsensitiveHashCodeProvider(myCul), New CaseInsensitiveComparer(myCul))
      myHT4.Add("FIRST", "Hello")
      myHT4.Add("SECOND", "World")
      myHT4.Add("THIRD", "!")

      ' Search for a key in each hashtable.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))
      Console.WriteLine("first is in myHT4: {0}", myHT4.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.  Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: True
'first is in myHT4: False

備註

CaseInsensitiveComparer 實 IComparer 作了支援字串大小寫不區分比較的介面,就像 CaseInsensitiveHashCodeProvider 實 IHashCodeProvider 作了支援字串大小寫不區分比較的介面一樣。

Important

不建議您將 CaseInsensitiveComparer 類別用於新的開發。 相反地,我們建議你使用 System.StringComparer 由 StringComparer.CurrentCultureIgnoreCase、 StringComparer.InvariantCultureIgnoreCase或 StringComparer.OrdinalIgnoreCase 屬性回傳的物件。

該 Comparer 類別是介面的預設實作 IComparer ,並執行大小寫區分字串的比較。

被 用作Hashtable鍵的物件必須覆蓋Object.GetHashCode方法(或介面IHashCodeProvider)與方法Object.Equals(或介面)。IComparer 兩種方法或介面的實作必須以相同方式處理大小寫敏感;否則,可能會 Hashtable 有異常行為。 例如,建立 時 Hashtable,必須將此類別與類別或 CaseInsensitiveHashCodeProvider 任何大小寫不區分 IHashCodeProvider 的實作一起使用。

弦比對可能因文化不同而有不同的結果。 欲了解更多文化特定比較資訊,請參閱 System.Globalization 命名空間及 全球化與在地化。

建構函式

名稱 Description
CaseInsensitiveComparer()

利用目前執行緒的 初始CaseInsensitiveComparer化該類別的新實例CurrentCulture。

CaseInsensitiveComparer(CultureInfo)

使用指定的 CaseInsensitiveComparer初始化類別的新實例CultureInfo。

屬性

名稱 Description
Default

會得到一個與當前執行緒相關聯CaseInsensitiveComparer且始終可用的實例CurrentCulture。

DefaultInvariant

會得到一個與CaseInsensitiveComparer相關聯且隨時可用的實例InvariantCulture。

方法

名稱 Description
Compare(Object, Object)

對兩個同類型物件進行大小寫不區分的比較,並回傳一個值,表示其中一個小於、等於或大於另一個。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱