Wednesday, August 29, 2007

Generics in C#

With .NET 2.0 , C# now has generic methods, generic classes, generic structures and generic interfaces. But in addition, it also has generic delegates

The question might arise as to why it was desirable to introduce generics in C#. One reason is that creating data structures like Stacks, Queues, ArrayLists, Hashtables and SortLIsts requires boxing and unboxing of data because these constructs are defined in terms of the object data type. Therefore when other data types are used in these data structure, it is necessary to use type conversion, i.e. boxing and unboxing. One of the problems with this process is that if these actions are preformed a large number of times, then they can use a large amount of clock time doing these processes. Further if the boxing and unboxing is performed incorrectly, then errors can occur. However the errors are usually runtime errors rather than compile time errors making it therefore more difficult to find and to correct. Generics in C# reduce the need for boxing and unboxing. As a result, the clock time requirements are reduced. In addition, if there are any errors in using the data, they may be more easily found at compile time rather than at run time making the program much more type safe.



Builtin Generic Interfaces and Generic Classes


The namespace: System.Collections.Generic contains several useful interfaces. Because of the frequency of the expected use of generics in C# programs, the following using appears by default above program created by the IDE:



using System.Collections.Generic;




The following table lists some of the generic interfaces that are included in this namespace:


ICollection

IComparable

IComparer

IDictionary

IEnumerable

IEnumerator

IList




Note: T is frequently used for the unknown data type, K is used for the unknown key type and V is used for the unknown value type. If more than one unknown is required, then the letters: R and S are used.

Several of the generic interfaces listed above have non-generic interface counter parts that were discussed earlier in the lectures.

In addition to the generic interfaces listed above, C# has several generic classes built in as well. These classes are also included in the namespace: System.Collections.Generic. The following table lists some of these builtin generic classes as well as their non-generic counterparts:


Open table as spreadsheet Generic Class
NonGeneric Conterpart in System.Collection

Collection
CollectionBase

Comparer
Comparer

Dictionary
HashTable

List
ArrayList

LinkedList

Queue
Queue

SortedDictionary
SortedList

Stack
Stack




For example the generic class List above could be used to manage a list of any data type. Some of the methods for this class are:


Open table as spreadsheet Method
Purpose

Add()
Adds elements to the list

BinarySearch()
Locates a particular element in the list returning a position

Contains()
Determine if an element is in the list returning a bool

Remove()
Removes an element and returns a bool to indicate whether it was removed.

this[int index]
An indexer with {get; set;}




Suppose that you wanted to create a list of objects for the class: People. Using List, this could be done by the following definition:



List theCustomers = new List();




This List could therefore be managed as in the console program: CustomersList.cs. Notice how this program works similar to using an ArrayList.





Generic Methods




Generic Classes




Generic Class Inheritance from Classes and Interfaces

No comments: