Generic Collections in ASP.NET C# Framework

Collections such as ArrayList does not distinguish between strings, integers, object they store every value as object and we need to cast them while iterating through the array. Sometimes we need to have homogeneous lists of integers only or strings only. This cannot not be done usingArrayList or other collections. In most cases we had to write our custom lists. However, with the introduction of .NET 2.0 Framework this tasks is very easy and we can employ so called generics for this specific purpose.

Generics exist for all types of collections. The way we use collection we specify type of the values that will be stored inside collection and then use all standard functions of this collection
// C#
List<int> intMyList = new List<int>();
intMyList.Add(1);
intMyList.Add(2);
intMyList.Add(3);
int number = intMyList[0];
foreach (int i in intMyList)
{
    Console.WriteLine(i);
}

 

TypeGeneric Type
ArrayList List<>; <int>; <string>
Queue Queue<>;<int>;<string>
Stack Stack<>;<int>;<string>
Hashtable Dictionary<>;<int, string>
SortedList SortedList<>;<int, string>
ListDictionary Dictionary<>;<int, string>
HybridDictionary Dictionary<> ;<int, string>
OrderedDictionary Dictionary<> ;<int, string>
SortedDictionary SortedDictionary<> ;<int, string>
NameValueCollection Dictionary<> ;<int, string>
DictionaryEntry NameValuePair<>;<string, int>
StringCollection List<String>
StringDictionary Dictionary<String>
N/A LinkedList<>

The exception to this rule is LinkedList Class which is new generic collection in .NET Framework. LinkedList Generic class exhibits following functionality.

Properties

 

NameDescription
Count # of nodes in LinkedList
First 1st node in LinkedList
Last The last node in LinkedList

Methods

 

NameDescription
AddAfter Inserts a new node after
AddBefore Inserts a new node before
AddFirst Inserts a new node at the beginning
AddLast Inserts a new node at the end
Clear Clears all nodes
Contains Checks to see if value is contained
CopyTo Copies the array
Find Finds values
FindLast Find last value
Remove Deletes the first occurrence
RemoveFirst Deletes the first item
RemoveLast Deletes the last item from LinkedList

LinkedList contains a collection of LinkedListNode objects with the following properties.

Properties

 

NameDescription
List Gets the LinkedList
Next Gets the next node
Previous Gets the previous node
Value Gets the value in the node

// C#
LinkedList<String> myLinks = new LinkedList<string>();
LinkedListNode<string> first = myLinks.AddLast("First");
LinkedListNode<string> last = myLinks.AddFirst("Last");
LinkedListNode<string> second = myLinks.AddBefore(last, "Second");
myLinks.AddAfter(second, "Third");
foreach (string s in myLinks)
{
    Console.WriteLine(s);
}

Generic Collections also implement interfaces that can be generic as well. We all know interface such as IEnumerable, ICollection and IList and etc… and generic collections implement these interfaces and they also can implement generic versions of these interfaces.
// C#
List<String> myStringList = new List<String>();
//Nongeneric
IList theMyList = (IList)myStringList;
object firstItem = theMyList[0];
//Generic with type of String
IList<String> typeSafeList = (IList<String>) myStringList;
String firstString = typeSafeList[0];
//Same type of concepts applies to Enumerators
List<string>.Enumerator e = myStringList.GetEnumerator();
while (e.MoveNext())
{
    // Typesafe Access to the current item
    string s = e.Current;
}

And same for generic comparisons they are generic Comparer class and generic EqualityComparer class
// C#
class MyComparer<T>: Comparer<T>
{
    public override int Compare(T x, T y)
    {
        return x.GetHashCode() - y.GetHashCode();
    }
}