New Features in C# 2.0—Generic Collection: How do I do that?
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
How do I do that?
The easiest way to create a generic collection class is to create a specific collection (for example, one that holds integers) and then replace the type (for example, int) with the generic type (for example, T).
Thus:
private int data;
becomes:
private T data; // T is a generic Type Parameter
The generic type parameter (in this case, T) is defined by you when you
create your collection class by placing the type parameter inside angle
brackets (< >):
public class Node<T>
| TIP |
Many programmers use T for "type," but Microsoft recommends you use longer, more descriptive type names (for example, Node<DocumentType>).
|
Now you have defined a new type, "Node of T," which at runtime will
become "Node of int" or node of any other type the compiler recognizes.
Example 1-2 creates a linked list of nodes of T, and then uses two instances of that generic list, each holding a different type of object.
Example 1-2. Creating your own generic collection
using System; namespace GenericLinkedList { public class Pilgrim { private string name; public Pilgrim(string name) { this.name = name; } public override string ToString( ) { return this.name; } } public class Node<T> { // member fields private T data; private Node<T> next = null; // constructor public Node(T data) { this.data = data; } // properties public T Data { get { return this.data; } } public Node<T> Next { get { return this.next; } } // methods public void Append(Node<T> newNode) { if (this.next = = null) { this.next = newNode; } else { next.Append(newNode); } } public override string ToString( ) { string output = data.ToString( ); if (next != null) { output += ", " + next.ToString( ); } return output; } } // end class public class LinkedList<T> { // member fields private Node<T> headNode = null; // properties // indexer public T this[int index] { get { int ctr = 0; Node<T> node = headNode; while (node != null && ctr <= index) { if (ctr = = index) { return node.Data; } else { node = node.Next; } ++ctr; } // end while throw new ArgumentOutOfRangeException( ); } // end get } // end indexer // constructor public LinkedList( ) { } // methods public void Add(T data) { if (headNode = = null) { headNode = new Node<T>(data); } else { headNode.Append(new Node<T>(data)); } } public override string ToString( ) { if (this.headNode != null) { return this.headNode.ToString( ); } else { return string.Empty; } } } class Program { static void Main(string[ ] args) { LinkedList<int> myLinkedList = new LinkedList<int>( ); for (int i = 0; i < 10; i++) { myLinkedList.Add(i); } Console.WriteLine("Integers: " + myLinkedList); LinkedList<Pilgrim> pilgrims = new LinkedList<Pilgrim>( ); pilgrims.Add(new Pilgrim("The Knight")); pilgrims.Add(new Pilgrim("The Miller")); pilgrims.Add(new Pilgrim("The Reeve")); pilgrims.Add(new Pilgrim("The Cook")); Console.WriteLine("Pilgrims: " + pilgrims); Console.WriteLine("The fourth integer is " + myLinkedList[3]); Pilgrim d = pilgrims[1]; Console.WriteLine("The second pilgrim is " + d); } } }
Output:
Integers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Pilgrims: The Knight, The Miller, The Reeve, The Cook
The fourth integer is 3
The second pilgrim is The Miller
|

