Generic Collections in ASP.NET C# Framework
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);
}
Type | Generic 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
Name | Description |
---|---|
Count | # of nodes in LinkedList |
First | 1st node in LinkedList |
Last | The last node in LinkedList |
Methods
Name | Description |
---|---|
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
Name | Description |
---|---|
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();
}
}