CollectionsUtil Class

CollectionsUtil Class

 

Creates collections that ignore the case in strings.

Namespace:   System.Collections.Specialized
Assembly:  System (in System.dll)

System::Object
  System.Collections.Specialized::CollectionsUtil

public ref class CollectionsUtil 

NameDescription
System_CAPS_pubmethodCollectionsUtil()

Initializes a new instance of the CollectionsUtil class.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateCaseInsensitiveHashtable()

Creates a new case-insensitive instance of the Hashtable class with the default initial capacity.

System_CAPS_pubmethodSystem_CAPS_staticCreateCaseInsensitiveHashtable(IDictionary^)

Copies the entries from the specified dictionary to a new case-insensitive instance of the Hashtable class with the same initial capacity as the number of entries copied.

System_CAPS_pubmethodSystem_CAPS_staticCreateCaseInsensitiveHashtable(Int32)

Creates a new case-insensitive instance of the Hashtable class with the specified initial capacity.

System_CAPS_pubmethodSystem_CAPS_staticCreateCaseInsensitiveSortedList()

Creates a new instance of the SortedList class that ignores the case of strings.

System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

These methods generate a case-insensitive instance of the collection using case-insensitive implementations of the hash code provider and the comparer. The resulting instance can be used like any other instances of that class, although it may behave differently.

For example, suppose two objects with the keys "hello" and "HELLO" are to be added to a hash table. A case-sensitive hash table would create two different entries; whereas, a case-insensitive hash table would throw an exception when adding the second object.

The following example uses two collections, a hash table and a sorted list, to hold population values for a group of cities. The values are retrieved from the collections by using the city names as keys. The city names are in mixed case to show their use as case-insensitive keys.

#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

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1

A Hashtable can support one writer and multiple readers concurrently. To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.

A SortedList can support multiple readers concurrently, as long as the collection is not modified. To guarantee the thread safety of the SortedList, all operations must be done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Return to top
Show:
© 2016 Microsoft