CollectionsUtil 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建忽略字符串大小写的集合。
public ref class CollectionsUtil
public class CollectionsUtil
type CollectionsUtil = class
Public Class CollectionsUtil
- 继承
-
CollectionsUtil
示例
以下示例使用两个集合(一个哈希表和一个排序列表)来保存一组城市的人口值。 使用城市名称作为键从集合中检索值。 城市名称采用混合大小写,以将其用作不区分大小写的键。
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
ref class TestCollectionsUtils
{
public:
static void Main()
{
Hashtable^ population1 = CollectionsUtil::CreateCaseInsensitiveHashtable();
population1["Trapperville"] = 15;
population1["Doggerton"] = 230;
population1["New Hollow"] = 1234;
population1["McHenry"] = 185;
// Select cities from the table using mixed case.
Console::WriteLine("Case insensitive hashtable results:\n");
Console::WriteLine("{0}'s population is: {1}", "Trapperville", population1["trapperville"]);
Console::WriteLine("{0}'s population is: {1}", "Doggerton", population1["DOGGERTON"]);
Console::WriteLine("{0}'s population is: {1}", "New Hollow", population1["New hoLLow"]);
Console::WriteLine("{0}'s population is: {1}", "McHenry", population1["MchenrY"]);
SortedList^ population2 = CollectionsUtil::CreateCaseInsensitiveSortedList();
for each (String^ city in population1->Keys)
{
population2->Add(city, population1[city]);
}
// Select cities from the sorted list using mixed case.
Console::WriteLine("\nCase insensitive sorted list results:\n");
Console::WriteLine("{0}'s population is: {1}", "Trapperville", population2["trapPeRVille"]);
Console::WriteLine("{0}'s population is: {1}", "Doggerton", population2["dOGGeRtON"]);
Console::WriteLine("{0}'s population is: {1}", "New Hollow", population2["nEW hOLLOW"]);
Console::WriteLine("{0}'s population is: {1}", "McHenry", population2["MchEnrY"]);
}
};
int main()
{
TestCollectionsUtils::Main();
}
// This program displays the following output to the console
//
// Case insensitive hashtable results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
//
// Case insensitive sorted list results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
using System;
using System.Collections;
using System.Collections.Specialized;
class TestCollectionsUtils
{
public static void Main()
{
Hashtable population1 = CollectionsUtil.CreateCaseInsensitiveHashtable();
population1["Trapperville"] = 15;
population1["Doggerton"] = 230;
population1["New Hollow"] = 1234;
population1["McHenry"] = 185;
// Select cities from the table using mixed case.
Console.WriteLine("Case insensitive hashtable results:\n");
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population1["trapperville"]);
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population1["DOGGERTON"]);
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population1["New hoLLow"]);
Console.WriteLine("{0}'s population is: {1}", "McHenry", population1["MchenrY"]);
SortedList population2 = CollectionsUtil.CreateCaseInsensitiveSortedList();
foreach (string city in population1.Keys)
{
population2.Add(city, population1[city]);
}
// Select cities from the sorted list using mixed case.
Console.WriteLine("\nCase insensitive sorted list results:\n");
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population2["trapPeRVille"]);
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population2["dOGGeRtON"]);
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population2["nEW hOLLOW"]);
Console.WriteLine("{0}'s population is: {1}", "McHenry", population2["MchEnrY"]);
}
}
// This program displays the following output to the console
//
// Case insensitive hashtable results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
//
// Case insensitive sorted list results:
//
// Trapperville's population is: 15
// Doggerton's population is: 230
// New Hollow's population is: 1234
// McHenry's population is: 185
Imports System.Collections
Imports System.Collections.Specialized
Class TestCollectionsUtils
Public Shared Sub Main()
Dim population1 As Hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable()
population1("Trapperville") = 15
population1("Doggerton") = 230
population1("New Hollow") = 1234
population1("McHenry") = 185
' Select cities from the table using mixed case.
Console.WriteLine("Case insensitive hashtable results:" + Environment.NewLine)
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population1("trapperville"))
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population1("DOGGERTON"))
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population1("New hoLLow"))
Console.WriteLine("{0}'s population is: {1}", "McHenry", population1("MchenrY"))
Dim population2 As SortedList = CollectionsUtil.CreateCaseInsensitiveSortedList()
For Each city As String In population1.Keys
population2.Add(city, population1(city))
Next city
' Select cities from the sorted list using mixed case.
Console.WriteLine(Environment.NewLine + "Case insensitive sorted list results:" + Environment.NewLine)
Console.WriteLine("{0}'s population is: {1}", "Trapperville", population2("trapPeRVille"))
Console.WriteLine("{0}'s population is: {1}", "Doggerton", population2("dOGGeRtON"))
Console.WriteLine("{0}'s population is: {1}", "New Hollow", population2("nEW hOLLOW"))
Console.WriteLine("{0}'s population is: {1}", "McHenry", population2("MchEnrY"))
End Sub
End Class
' This program displays the following output to the console
'
' Case insensitive hashtable results:
'
' Trapperville's population is: 15
' Doggerton's population is: 230
' New Hollow's population is: 1234
' McHenry's population is: 185
'
' Case insensitive sorted list results:
'
' Trapperville's population is: 15
' Doggerton's population is: 230
' New Hollow's population is: 1234
' McHenry's population is: 185
注解
这些方法使用哈希代码提供程序和比较器不区分大小写的实现生成集合的不区分大小写的实例。 生成的实例可以与该类的任何其他实例一样使用,尽管它的行为可能不同。
例如,假设要向哈希表添加两个键为“hello”和“HELLO”的对象。 区分大小写的哈希表将创建两个不同的条目:而,不区分大小写的哈希表在添加第二个对象时会引发异常。
构造函数
CollectionsUtil() |
初始化 CollectionsUtil 类的新实例。 |
方法
CreateCaseInsensitiveHashtable() |
创建 Hashtable 类具有默认初始容量的不区分大小写的新实例。 |
CreateCaseInsensitiveHashtable(IDictionary) |
将项从指定字典复制到 Hashtable 类的不区分大小写的新实例,该实例具有与复制项的数量相同的初始容量。 |
CreateCaseInsensitiveHashtable(Int32) |
创建 Hashtable 类具有指定初始容量的不区分大小写的新实例。 |
CreateCaseInsensitiveSortedList() |
创建 SortedList 类的新实例,该实例忽略字符串的大小写。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
适用于
线程安全性
可以 Hashtable 同时支持一个编写器和多个读取器。 若要支持多个编写器,所有操作都必须通过 方法返回的 Synchronized(Hashtable) 包装器来完成。
SortedList只要集合未修改,就可以同时支持多个读取器。 若要保证 的 SortedList线程安全,所有操作都必须通过 方法返回的 Synchronized(SortedList) 包装器来完成。
通过集合枚举本质上不是线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。