What is generics In .net

• Use generic types to maximize code reuse, type safety, and performance.

• The most 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 in place of classes such as ArrayList in the System.Collections namespace.

• You can create your own generic interfaces, classes, methods, events and delegates.

• Generic classes may be constrained to enable access to methods on particular data types.

• Information on the types used in a generic data type may be obtained at run-time by means of reflection.


// Declare the generic class
public class GenericList
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int
GenericList list1 = new GenericList();

// Declare a list of type string
GenericList list2 = new GenericList();
// Declare a list of type ExampleClass
GenericList list3 = new GenericList();
}
}