Generic Overview:
You can use generic types to maximize code reuse, protect type security, and improve performance.
The most common use of generics is to create a collection class.
The. NET Framework class library contains several new Generic collection classes in the System. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as the ArrayList in the System. Collections namespace.
You can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegation.
You can restrict generic classes to access specific data types.
Information about types used in generic data types can be obtained at runtime through reflection.
Test code:
[Csharp]
// 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 ();
}
}
// 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 ();
}
}