ArticleDirectory
- 1. Overview of generics
- 2. Advantages of generics
- 3. generic type parameters
- 4. generics and Arrays
1. Overview of generics
- The generic type can be postponed to the client.CodeWhen declaring and instantiating a class.
- Using generic types can maximize code reuse, protect type security, and improveProgramPerformance.
- The most common use of generics is to create a collection class.
- The. NET Framework class library contains several generic collection classes in the system. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as arraylist and other collection classes in the system. Collections namespace.
2. Advantages of generics
- By creating a generic class, you can create a set of secure types during compilation (type check during compilation ).
- Code Description:
Code
System. Collections. arraylist list = New System. Collections. arraylist ();.
List. Add ( 3 );
List. Add ( " It is raining in Redmond. " );
Int T = 0 ;
Foreach ( Int X In List) // No exception is thrown here for compilation. An error is reported when the compilation process ends.
{
T + = X;
}
In the preceding Code, although the combination of strings and INT in an arraylist is completely legal when creating a heterogeneous set, arraylist forcibly converts all items to objects, however, this method is more likely to generate a programming error and cannot be detected at runtime.
- By creating generic classes, you can improve program performance and avoid unnecessary forced conversion, packing, and unpacking operations.
- Code Description:
Code
System. Collections. arraylist list1 = New System. Collections. arraylist ();
List1.add ( 3 );
List1.add ( 105 );
System. Collections. arraylist list2 = New System. Collections. arraylist ();
List2.add ( " It is raining in Redmond. " );
List2.add ( " It is snowing in the mountains. " );
The code in the preceding example shows that arraylist is a collection class that is very convenient to use. It can be used to store any reference or value type without modification. However, this kind of convenience requires a price. Any reference or value type added to the arraylist is implicitly forcibly converted to an object. If the item is of the value type, you must bind it to the list and unpack it during retrieval. Forced conversion, as well as packing and unpacking operations will reduce performance. When circular access is required for large sets, the effects of packing and unpacking are very obvious.
3. generic type parameters
A type parameter is a placeholder of a specific type specified by the client when instantiating a variable of the generic type. (Example below)
Code
Public Class Genericlist < T > // T is a generic type parameter, but when the specific type of T is specified in the client code, all t in the generic type are replaced with the specific type
{
// Define a nested class
Private Class Node
{
Public Node (T)
{
Next = Null ;
Data = T;
}
Private Node next;
Public Node next
{
Get { Return Next ;}
Set {Next = Value ;}
}
Private T data;
Public T data
{
Get { Return Data ;}
Set {Data = Value ;}
}
}
Private Node head; // Declare a variable of a nested class
Public Genericlist ()
{
Head = Null ;
}
// Method for adding a member to a generic class
Public Void Addhead (T)
{
Node n = New Node (t );
N. Next = Head;
Head = N;
}
// Used to traverse a generic class set
Public Ienumerator < T > Getenumerator ()
{
Node current = Head;
While (Current ! = Null )
{
Yield Return Current. Data;
Current = Current. Next;
}
}
}
Code
Static Void Main ( String [] ARGs)
{
Genericlist < Int > List = New Genericlist < Int > ();
For ( Int X = 0 ; X < 10 ; X ++ )
{
List. addhead (X );
}
Foreach ( Int I In List)
{
System. Console. Write (I + " " );
}
System. Console. writeline ( " \ Ndone " );
Console. Read ();
4. generics and Arrays
In C #2.0, the one-dimensional array with the lower limit of 0 is automatically implemented ilist <t>. This allows you to create generic methods that can access arrays and other collection types cyclically using the same code. Example
Code
Class Program
{
Static Void Main ()
{
Int [] Arr = { 0 , 1 , 2 , 3 , 4 };
List < Int > List = New List < Int > ();
For ( Int X = 5 ; X < 10 ; X ++ )
{
List. Add (X );
}
Processitems < Int > (ARR );
Processitems < Int > (List );
}
Static Void Processitems < T > (Ilist < T > Coll)
{
Foreach (T item In Coll)
{
System. Console. Write (item. tostring () + " " );
}
System. Console. writeline ();
}
}