共用方式為


CollectionsUtil 類別

定義

建立忽略大小寫的字串集合。

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) 回的包裝函式來完成。

透過集合列舉本質上不是安全線程程式。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱