CaseInsensitiveComparer 类
比较两个对象是否相等,比较时忽略字符串的大小写。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class CaseInsensitiveComparer
Implements IComparer
用法
Dim instance As CaseInsensitiveComparer
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class CaseInsensitiveComparer : IComparer
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class CaseInsensitiveComparer : IComparer
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public class CaseInsensitiveComparer implements IComparer
SerializableAttribute
ComVisibleAttribute(true)
public class CaseInsensitiveComparer implements IComparer
备注
CaseInsensitiveComparer 实现 IComparer 接口,该接口支持对字符串进行不区分大小写的比较,正如 CaseInsensitiveHashCodeProvider 实现 IHashCodeProvider 接口(该接口支持对字符串进行不区分大小写的比较)一样。
Comparer 类为 IComparer 接口的默认实现,并执行区分大小写的字符串比较。
要重写 Object.GetHashCode 方法(或 IHashCodeProvider 接口)和 Object.Equals 方法(或 IComparer 接口),需要有被 Hashtable 用作键的对象。两个方法或接口的实现必须以相同的方式处理大小写;否则,Hashtable 的行为可能不正确。例如,创建 Hashtable 时,您必须配合使用此类和 CaseInsensitiveHashCodeProvider 类或任何不区分大小写的 IHashCodeProvider 实现。
根据区域性的不同,字符串比较可能会有不同的结果。有关区域性特定比较的更多信息,请参见 System.Globalization 命名空间和 编码和本地化。
示例
下面的代码示例创建一个区分大小写的哈希表和一个不区分大小写的哈希表,并演示二者行为的差别,甚至是当二者包含相同的元素时各自行为的差别。
Imports System
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 'Main
End Class 'SamplesHashtable
'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
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
*/
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
// Create a Hashtable using the default hash code provider and the default comparer.
Hashtable^ myHT1 = gcnew 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 = gcnew Hashtable( gcnew CaseInsensitiveHashCodeProvider,gcnew 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 = gcnew 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 = gcnew CultureInfo( "tr-TR" );
Hashtable^ myHT4 = gcnew Hashtable( gcnew CaseInsensitiveHashCodeProvider( myCul ),gcnew 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
*/
import System.*;
import System.Collections.*;
import System.Globalization.*;
public class SamplesHashtable
{
public static void main(String[] args)
{
// 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.get_DefaultInvariant(),
CaseInsensitiveComparer.get_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}",
(System.Boolean)myHT1.ContainsKey("first"));
Console.WriteLine("first is in myHT2: {0}",
(System.Boolean)myHT2.ContainsKey("first"));
Console.WriteLine("first is in myHT3: {0}",
(System.Boolean)myHT3.ContainsKey("first"));
Console.WriteLine("first is in myHT4: {0}",
(System.Boolean)myHT4.ContainsKey("first"));
} //main
} //SamplesHashtable
/*
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
*/
继承层次结构
System.Object
System.Collections.CaseInsensitiveComparer
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
CaseInsensitiveComparer 成员
System.Collections 命名空间
IComparer
IComparable 接口
Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo
CaseInsensitiveHashCodeProvider