CollectionsUtil クラス

定義

文字列内の大文字と小文字を無視するコレクションを作成します。

public ref class CollectionsUtil
public class CollectionsUtil
type CollectionsUtil = class
Public Class CollectionsUtil
継承
CollectionsUtil

次の例では、ハッシュ テーブルと並べ替えられたリストの 2 つのコレクションを使用して、都市のグループの母集団の値を保持します。 値は、キーとして市区町村名を使用してコレクションから取得されます。 都市名は大文字と小文字が混在しており、大文字と小文字が区別されないキーとして使用されます。

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" を持つ 2 つのオブジェクトをハッシュ テーブルに追加するとします。 大文字と小文字を区別するハッシュ テーブルでは、2 つの異なるエントリが作成されます。一方、大文字と小文字を区別しないハッシュ テーブルでは、2 番目のオブジェクトを追加するときに例外がスローされます。

コンストラクター

名前 説明
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では、1 つのライターと複数のリーダーを同時にサポートできます。 複数のライターをサポートするには、 Synchronized(Hashtable) メソッドによって返されるラッパーを介してすべての操作を実行する必要があります。

コレクションが変更されない限り、 SortedList は複数のリーダーを同時にサポートできます。 SortedListのスレッド セーフを保証するには、Synchronized(SortedList) メソッドによって返されるラッパーを通じてすべての操作を実行する必要があります。

コレクションを通じて列挙することは、本質的にスレッド セーフなプロシージャではありません。 コレクションが同期されている場合でも、他のスレッドはコレクションを変更できるため、列挙子は例外をスローします。 列挙中のスレッド セーフを保証するには、列挙全体の間にコレクションをロックするか、他のスレッドによって行われた変更によって発生する例外をキャッチします。

こちらもご覧ください