Generic is C #2.0 Language and public Language Runtime Library (CLR) . Generics introduce the concept of type parameters . NET Framework The type parameter makes it possible to design the following classes and Methods: these classes and Methods delay the specified one or more types to the clientCodeWhen declaring and instantiating this class or method. For example T You can write a single class that can be used by other client code without introducing the forced conversion or packing operation during runtime. .
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.
. NET Framework Class Library in System. Collections. Generic The namespace contains several new generic collection classes. Use these classes as much as possible to replace common classes, such System. Collections In the namespace Arraylist , Hashtable .
The following describes the usage of several generic collection classes:
I . Dictionary And Hashtable Similar
Such . NET Framework 2.0 Is added. A set of keys and values. Namespace : System. Collections. Generic ,ProgramSet : Mscorlib (In Mscorlib. dll Medium)
Class testgenericlist
{
Static void main ()
{
// Declared object, Parameter Representation, key is Int Type, value is String Type
Dictionary <int, string> fruit = new dictionary <int, string> ();
Try {
// Adding duplicate keys causes an exception.
Fruit. Add (1 ," Apple ");
Fruit. Add (2 ," Orange ");
Fruit. Add (3 ," Bananas ");
Fruit. Add (4 ," Pineapple ");
// Parameter error will cause an exception , As shown below
// Fruit. Add ("5", "AA ");
}
Catch (argumentexception)
{
Console. writeline (" Adding Error !!! ");
}
// Because the generic type is introduced, you do not need to perform the key extraction. Object To Int The same is true for the set of values.
Foreach (int I in fruit. Keys)
{
Console. writeline (" Key: {0} Value: {1} ", I, fruit);
}
Key-Value Pair Traversal :
Foreach (keyvaluepair keyValue in Fruit)
{
Console. writeline (" Key: {0} Value: {1} ", keyValue. Key, keyValue. value );
}
// Deletes a specified key, Value
Fruit. Remove (1 );
// Determines whether the specified key is included.
If (fruit. containskey (1 ))
{
Console. writeline (" Include this key ");
}
// Clear all objects in the Set
Fruit. Clear ();
}
}
Dictionary The order of traversal output is the order of addition. Hashtable Different, other methods such: Containskey , Containsvalue , Remove .
II, List Class
Note: . NET Framework 2.0 Is added. Lists the strong types of objects that can be accessed through indexes. Provides methods for searching, sorting, and operating the list. Namespace : System. Collections. Generic , Assembly : Mscorlib (In Mscorlib. dll ), List Class is Arraylist Class generic equivalent class.
// Declare a generic class
Class testgenericlist
{
Static void main ()
{
// Declare List Object and Arraylist Corresponding , Add only String Parameters
List <string> names = new list <string> ();
Names. Add (" Qiao Feng ");
Names. Add (" Ouyang Feng ");
Names. Add (" Ma Feng ");
// Traversal List
Foreach (string name in names)
{
Console. writeline (name );
}
// Direction List Insert element
Names. insert (2 ," Zhang Sanfeng ");
// Removes a specified element.
Names. Remove (" Ma Feng ");
}
}
When deciding to use List Still use Arraylist Class (both have similar functions), remember List Class performs better in most cases and is type-safe. If List Class type T When the reference type is used, the actions of the two classes are identical. However, if T To use the value type, you must consider implementation and packing.
IfTWhen the value type is used, the compiler will generateListClass implementation. This means you do not haveListThe list element of the object can be used after it is boxed, and500After list elements, the memory saved by packing the list elements will be greater than the memory used to generate this class.
You can also define a generic class as follows:
// Declare a generic class
Public class itemlist <t>
{
Void add (T item ){}
}
Class testgenericlist
{
Private class exampleclass {}
Static void main ()
{
// Declare an object. Only one object can be added. Int Type
Itemlist <int> list1 = new itemlist <int> ();
// declares an object, only Student type, Student the class is a custom class
itemlist list2 = new itemlist ();
}
}
There are many other generic methods, such as generic methods, generic delegation, and generic interfaces.