Generics are the new feature in the c# 2.0. They introduce the concept of Type parameter. This makes it possible to classes and methods that differs the specification of one or more types until the class or method is declared and used by the code, which will be using it.
Generic helps us to maximize the reuse of code, Type safety and better performance. Generics are most commonly used to create a collection. There are many new generic collection classes in the System.collection.Generics namespace. These classes are advised to be used for type safety and better performance over araylist.
We can also create our own Generic interface, classes, methods, events and delegates. Information on the types used in a generic data type may be obtained at run-time by means of reflection
We can declare a generic class like this
public class GenericClass
{
void Add(T input) { }
}
We create an instance of the generic class like this
GenericClass
Here if want we can also initialize the class to be of any other type like a user defined class. For example
Public class test
{
Pubic class TestClass{}
GenericClass
}
No comments:
Post a Comment