Specialized Collections
Introduction to Specialized Collections
In C#, the System.Collections.Specialized namespace provides specialized and strongly-typed collections. These collections are designed for specific tasks that are not directly supported by the general-purpose collection classes. In this tutorial, we will explore various types of specialized collections in C#.
ListDictionary
The ListDictionary
is a collection that represents a linked list of key/value pairs. This type of collection is optimal for collections that typically contain 10 items or fewer.
Here is how to create and use a ListDictionary
:
ListDictionary listDictionary = new ListDictionary();
listDictionary.Add("1", "One");
listDictionary.Add("2", "Two");
foreach (DictionaryEntry entry in listDictionary)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
HybridDictionary
The HybridDictionary
is a collection that starts as a ListDictionary
and becomes a Hashtable
when the number of elements exceeds a certain threshold.
Here is how to create and use a HybridDictionary
:
HybridDictionary hybridDictionary = new HybridDictionary();
hybridDictionary.Add("1", "One");
hybridDictionary.Add("2", "Two");
foreach (DictionaryEntry entry in hybridDictionary)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
OrderedDictionary
The OrderedDictionary
is a collection that maintains the insertion order of elements.
Here is how to create and use an OrderedDictionary
:
OrderedDictionary orderedDictionary = new OrderedDictionary();
orderedDictionary.Add("1", "One");
orderedDictionary.Add("2", "Two");
foreach (DictionaryEntry entry in orderedDictionary)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
StringCollection
The StringCollection
is a collection that contains a collection of String
objects.
Here is how to create and use a StringCollection
:
StringCollection stringCollection = new StringCollection();
stringCollection.Add("One");
stringCollection.Add("Two");
foreach (string str in stringCollection)
{
Console.WriteLine(str);
}
StringDictionary
The StringDictionary
is a collection with keys and values of type String
.
Here is how to create and use a StringDictionary
:
StringDictionary stringDictionary = new StringDictionary();
stringDictionary.Add("1", "One");
stringDictionary.Add("2", "Two");
foreach (DictionaryEntry entry in stringDictionary)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
NameValueCollection
The NameValueCollection
is a collection that associates String
keys with collections of String
values.
Here is how to create and use a NameValueCollection
:
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add("Numbers", "One");
nameValueCollection.Add("Numbers", "Two");
foreach (string key in nameValueCollection.Keys)
{
Console.WriteLine($"{key}: {string.Join(", ", nameValueCollection.GetValues(key))}");
}
Each of these collections serves a different purpose and has its own advantages. The choice of which specialized collection to use depends on the specific requirements of the application.
In summary, specialized collections in C# are a powerful tool for handling data. They offer unique features that are not available in general-purpose collections. Understanding these collections and knowing when to use each one can greatly enhance your C# programming skills.
Happy coding!