CaseInsensitiveComparer 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
比较两个对象的等效性,忽略字符串大小写。
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在字符串上实现支持不区分大小写的比较的接口,就像实现IHashCodeProvider支持字符串不区分大小写的比较的接口一样CaseInsensitiveHashCodeProvider。
Important
不建议将 CaseInsensitiveComparer 类用于新开发。 相反,我们建议使用System.StringComparer由StringComparer.CurrentCultureIgnoreCase或StringComparer.InvariantCultureIgnoreCaseStringComparer.OrdinalIgnoreCase属性返回的对象。
该 Comparer 类是接口的默认实现 IComparer ,并执行区分大小写的字符串比较。
重写方法(或接口)和Object.Equals方法(或IComparerIHashCodeProvider接口)需要Object.GetHashCode用作Hashtable键的对象。 这两种方法或接口的实现必须以相同的方式处理区分大小写:否则,其 Hashtable 行为可能不正确。 例如,创建 a Hashtable时,必须将此类与类或任何不区分大小写IHashCodeProvider的实现一起使用CaseInsensitiveHashCodeProvider。
字符串比较可能具有不同的结果,具体取决于区域性。 有关区域性特定比较的详细信息,请参阅 System.Globalization 命名空间 和全球化和本地化。
构造函数
| 名称 | 说明 |
|---|---|
| CaseInsensitiveComparer() |
使用CurrentCulture当前线程初始化类的新实例CaseInsensitiveComparer。 |
| CaseInsensitiveComparer(CultureInfo) |
使用指定的CaseInsensitiveComparer值初始化类的新实例CultureInfo。 |
属性
| 名称 | 说明 |
|---|---|
| Default |
获取与当前线程关联的CurrentCulture实例,该实例CaseInsensitiveComparer始终可用。 |
| DefaultInvariant |
获取与该实例关联的InvariantCulture实例,该实例CaseInsensitiveComparer始终可用。 |
方法
| 名称 | 说明 |
|---|---|
| Compare(Object, Object) |
对同一类型的两个对象执行不区分大小写的比较,并返回一个值,该值指示一个对象是小于、等于还是大于另一个对象。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |