C # programming guide:
Http://msdn.microsoft.com/zh-cn/library/512aeb7t (vs.80). aspx Generic (C # programming guide)
Generic is a new function in the C # language and Common Language Runtime Library (CLR) of version 2.0. Generic Type introduces the concept of type parameters to. NET Framework. type parameters make it possible to design the following classes and Methods: these classes and Methods delay the specified one or more types to the client.CodeWhen declaring and instantiating this class or method. For example, by using the generic type parameter T, you can write a single class that can be used by other client code without introducing the cost or risk of force conversion or packing during runtime, as shown below:
C # copy code
// 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> ();}}
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 through reflection at runtime.
Related chapters
For more information:
Generic introduction (C # programming guide)
Advantages of generics (C # programming guide)
Generic parameters (C # programming guide)
Type parameter constraints (C # programming guide)
Generic class (C # programming guide)
Generic interface (C # programming guide)
Generic method (C # programming guide)
Generic delegation (C # programming guide)
Default keywords in generic code (C # programming guide)
Differences between C ++ templates and C # generics (C # programming guide)
Generic and reflection (C # programming guide)
Generics in the Runtime Library (C # programming guide)
Generic In the. NET Framework class library (C # programming guide)
"Generic" example (C #)
C # Language Specification
For more information, see the following sections in the C # Language Specification:
See Reference