Welcome to Kanbal.com
25 November 2008
Introduction
What are generics?
Generics are classes, structures, interfaces, methods that have placeholders for one or more types they store.
Example:
{
}
Here
The letter T can be replaced with any word. The above class is only a template, so we cannot create an instance for this class. We can create the instance only after replace the T with any specific Type.
Example
ExampleForGeneric<SomeOtherClassName> objExampleForGeneric = new ExampleForGeneric<SomeOtherClassName>();
This is the way, we can create instance to this class.
Why Generics?
Let's take a simple example with Array List
A single ArrayList can support any type
ArrayList objAL = new ArrayList();
objAL.Add("Kannan");
objAL.Add(222);
objAL.Add(myobj); //here myObj is a object of one of my other class.
for (int iCounter = 0; iCounter < objAL.Count; iCounter++)
{
String myString = objAL[iCounter]; // this will through exception at runtime.
}
Now we have added three different types to this ArrayList. Inorder to read them back, we have used a for loop. When we compile this application, we will not get any compile time exception. However, while executing the application we will get runtime exception because we are trying to assign myobj to a String.
To avoid this error we should read them back to Object. Again, boxing and unboxing is also overhead to the CLR.
This is the right time to use Generics. Lets see an another example with Generics.
List<String> objList = new List<String>();
objList.Add("Kannan");
objList.Add("Amutha");
objList.Add("Chandrasekaran");
for (int iCounter = 0; iCounter < objList.Count; iCounter++)
{
Console.WriteLine(objList[iCounter]);
}
In the above example, we are strongly typing the List as string. So if we add any other type apart from String then the compiler will through exception. Because the List<String> is a Strong Typed Class and there is no need for a boxing and unboxing. So with generics we have avoided both runtime exception and boxing, unboxing.
Generic Method
A Generic method might use type parameter as its return value or as the type is one of the formal parameter.
Here is the simple example for Generic Method
public class ExampleForGenerics
{
private T GenericMethod
{
T someValue = arg;
}
In the above class the method GenericMethod is a geniric method.
Generic method can appear in a non-generic type also. It is important to note that a method is not a generic just because of it belongs to a geniric type, or even its has a formal parameters whose types are generic parameters of the enclosing type.
A method is generic only if it has its own list of type parameters. In the following code, only the method Generic is generic method.
public class SimpleClass
{
T Generic
{
}
}
public class GenericClass
{
T NonGeneric(T args)
{
}
}
Microsoft provided few namespace for Generics in .net 2.0
System.Collections.Generic offers following classes
- Dictionary
- List
- Queue
- Stack
- Sorted List
- Linked List
System.Collections.ObjectModel offers
- Collections
- ReadOnlyCollections
- KeyedCollections
Other Generic Types
- Nullable types
- Array Segment
- Event Handlers
Here is the simple comparision with Generic and Non-Generic objects
|
Object |
Generic |
|
Runtime check |
Compile time checks |
|
Weakly types objects |
Strongly typed objects |
|
Required boxing and unboxing for value typed objects |
Reduce the need for boxing |
So for we have seen the generics in .net 2.0 and its use.
If a data structure or utility class offers a generic version, use generic versions
If your code target to .Net 1.1, then you should not use Generics.
