Applying Generics


Because of the native support for generics in the IL and the CLR, all CLR 2.0-compliant languages can take advantage of generic types. For example, here is some Visual Basic 2005 code that uses the generic stack of Example D-2:

     Dim stack As Stack(Of Integer)     stack = new Stack(Of Integer)     stack.Push(3)     Dim number As Integer     number = stack.Pop( ) 

You can use generics in classes and in structs. Here is a useful generic Point struct:

     public struct Point<T>     {        public T X;        public T Y;     } 

You can use the generic Point for integer coordinates:

     Point<int> point;     point.X = 1;     point.Y = 2; 

or for charting coordinates that require floating-point precision:

     Point<double> point;     point.X = 1.2;     point.Y = 3.4; 

Multiple Generic Types

A single type can define multiple generic type parameters. For example, consider the generic linked list shown in Example D-3.

Example D-3. A generic linked list
 class Node<K,T> {    public K Key;    public T Item;    public Node<K,T> NextNode;    public Node( )    {       Key      = default(K);       Item     = default(T);       NextNode = null;    }    public Node(K key,T item,Node<K,T> nextNode)    {       Key      = key;       Item     = item;       NextNode = nextNode;    } } public class LinkedList<K,T> {    Node<K,T> m_Head;         public LinkedList( )    {       m_Head = new Node<K,T>( );    }    public void AddHead(K key,T item)    {       Node<K,T> newNode = new Node<K,T>(key,item,m_Head.NextNode);       m_Head.NextNode = newNode;    } } 

The linked list stores nodes:

     class Node<K,T>     {...} 

Each node contains a key (of the generic type parameter K) and a value (of the generic type parameter T). Each node also has a reference to the next node in the list. The linked list itself is defined in terms of the generic type parameters K and T:

     public class LinkedList<K,T>     {...} 

This allows the list to expose generic methods such as AddHead( ):

     public void AddHead(K key,T item); 

Whenever you declare a variable of a type that uses generics, you must specify the types to use. However, the specified types can themselves be generic types. For example, the linked list has a member variable called m_Head of type Node<K,T>, used for referencing the first item in the list. m_Head is declared using the list's own generic type parameters K and T:

     Node<K,T> m_Head; 

You need to provide specific types when instantiating a node, and again, you can use the linked list's own generic type parameters:

     public void AddHead(K key,T item)     {        Node<K,T> newNode = new Node<K,T>(key,item,m_Head.NextNode);        m_Head.NextNode = newNode;     } 

Note that it is purely for readability purposes that the list uses the same names as the node for the generic type parameters is purely for readability purposes. It could have used other names, such as:

     public class LinkedList<U,V>     {...} 

or:

     public class LinkedList<KeyType,DataType>     {...} 

In which case, m_Head would have been declared as:

     Node<KeyType,DataType> m_Head; 

When the client uses the linked list, it has to provide specific types. The client can choose integers as keys and strings as data items:

     LinkedList<int,string> list = new LinkedList<int,string>( );     list.AddHead(123,"AAA"); 

However, it can also choose any other combination, such as a timestamp for keys:

     LinkedList<DateTime,string> list = new LinkedList<DateTime,string>( );     list.AddHead(DateTime.Now,"AAA"); 



Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net