Generics were firstly added to v 2.0 of the C#
language and the common language runtime (CLR). Generics represent to the .NET
Framework the concept of type parameters, which make it possible to design
classes and methods that defer the specification of one or more types until the
class or method is declared and instantiated by client code. For example, by
using a generic type parameter T you can write a single class that other
client code can use without incurring the cost or risk of runtime casts or
boxing operations, as shown here:
You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. A simple example would help understanding the concept:
// declare the generic class.
public class GenericList<T>
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
//
Declare a list of type int.
GenericList<int> list1 = new GenericList<int>();
// Declare a list of type string.
GenericList<string> list2 = new GenericList<string>();
// Declare a list of type ExampleClass.
GenericList<ExampleClass>
list3 = new GenericList<ExampleClass>();
}
}
Features of Generics
·
Generic types help to maximize code reuse, type
safety, and performance.
·
Common use of generics is to create collection
classes. The .NET Framework class library contains several new generic
collection classes in the System.Collections.Generic
namespace. These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.
·
You can make your own generic interfaces,
classes, methods, events and delegates.
·
Generic classes may be constrained to enable
access to methods on particular data types.
·
You can get information on the types that are
used in a generic data type may be obtained at run-time by using reflection.
You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. A simple example would help understanding the concept:
using System; using System.Collections.Generic; namespace GenericApplication { public class MyGenericArray<T> { private T[] array; public MyGenericArray(int size) { array = new T[size + 1]; } public T getItem(int index) { return array[index]; } public void setItem(int index, T value) { array[index] = value; } } class Tester { static void Main(string[] args) { //declaring an int array MyGenericArray<int> intArray = new MyGenericArray<int>(5); //setting values for (int c = 0; c < 5; c++) { intArray.setItem(c, c*5); } //retrieving the values for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); //declaring a character array MyGenericArray<char> charArray = new MyGenericArray<char>(5); //setting values for (int c = 0; c < 5; c++) { charArray.setItem(c, (char)(c+97)); } //retrieving the values for (int c = 0; c< 5; c++) { Console.Write(charArray.getItem(c) + " "); } Console.WriteLine(); Console.ReadKey(); } } }When the above code is compiled and executed, it produces the following result:
0 5 10 15 20 a b c d e
We will be discuss more about Generics in our next post.