Collections Util Class

The CollectionsUtil class provides static methods for creating collections that ignore the case in strings. The methods provided by the CollectionsUtil class create case-insensitive instances of Hashtable and SortedList classes. These methods generate a case-insensitive instance of the collection by using case-insensitive implementations of the hash code provider and the Comparer class. The resulting instance can be used like any other instances of that class.

In the following code example, you:

1. Create a case-insensitive string typed Hashtable.

2. Add three key/value pairs. Note that the keys contain case-insensitive strings.

3. Check if a specific key has been added to the Hashtable and then remove it.

4. Use the for each logic to iterate through the collection and display the keys and values that the collection contains.

Visual Basic

Dim cart As Hashtable = _

CollectionsUtil.CreateCaseInsensitiveHashtable cart.Add("ID-1", _

"Item 4") If cart.ContainsKey( _ "id-3") Then cart.Remove( _ "ID-3")

End If

DictionaryEntry _ In cart

Console.WriteLine( _ "{0} : {1}", _ de.Key, _ de.Value)


Hashtable cart = CollectionsUtil

.CreateCaseInsensitiveHashtab1e(); cart.Add("ID-1",

"Item 4"); if (cart.ContainsKey ("id-3"))

cart.Remove("ID-3"); foreach

(DictionaryEntry de in cart)

Console.WriteLine ("{0} : {1}", de.Key, de.Value);

Was this article helpful?

0 0

Post a comment