C # generics (Generic)
Generics (Generic) allow you to defer writing a specification of the data type of a programming element in a class or method until you actually use it in your program. In other words, generics allow you to write a class or method that can work with any data type.
You can write the specification of a class or method by substituting parameters for the data type. When the compiler encounters a function call to a class's constructor or method, it generates code to handle the specified data type. The following simple example will help you understand this concept:
Using system;using system.collections.generic;namespace genericapplication{public class Mygenericarray<t> { Private t[] array; public Mygenericarray (int size) {array = new T[size + 1]; } public T getItem (int index) {return array[index]; } public void SetItem (int index, T value) {Array[index] = value; }} class Tester {static void Main (string[] args) {//declares an integer array M ygenericarray<int> intarray = new mygenericarray<int> (5); Set the value for (int c = 0; c < 5; C + +) {Intarray.setitem (c, c*5); }//Gets the value for (int c = 0; c < 5; C + +) {Console.Write (Intarray.getitem (c ) + " "); } Console.WriteLine (); Declares a character array mygenericarray<char> chararray = new MygeneriCarray<char> (5); Set the value for (int c = 0; c < 5; C + +) {Chararray.setitem (c, (char) (c+97)); }//Gets the value for (int c = 0; c < 5; C + +) {Console.Write (chararray.getit EM (c) + ""); } Console.WriteLine (); Console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
0 5 20a b c D E
Characteristics of generics (Generic)
Using generics is a technique that enhances program functionality in the following ways:
It helps you to maximize code reuse, secure types of security, and improve performance.
You can create generic collection classes. The. NET Framework class library contains some new generic collection classes in the System.Collections.Generic namespace. You can use these generic collection classes to override the collection classes in System.Collections.
You can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegates.
You can constrain a generic class to access a method of a particular data type.
Information about the types used in a generic data type can be obtained at run time by using reflection.
Generic (Generic) method
In the above example, we have used a generic class, and we can declare a generic method from a type parameter. The following procedure illustrates this concept:
Using system;using System.collections.generic;namespace genericmethodappl{class Program {static void SWAP&L T T> (ref T LHS, ref t RHS) {T temp; temp = LHS; LHS = RHS; RHS = temp; } static void Main (string[] args) {int A, B; Char C, D; A = 10; b = 20; c = ' I '; d = ' V '; Displays the value Console.WriteLine before swapping ("Int values before calling swap:"); Console.WriteLine ("A = {0}, B = {1}", A, b); Console.WriteLine ("Char Values before calling swap:"); Console.WriteLine ("C = {0}, D = {1}", C, D); Call Swap swap<int> (ref a, ref B); Swap<char> (ref c, ref D); Displays the value Console.WriteLine after the interchange ("Int values after calling swap:"); Console.WriteLine ("A = {0}, B = {1}", A, b); Console.WriteLine ("Char Values after calling SWAP: "); Console.WriteLine ("C = {0}, D = {1}", C, D); Console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
Int values before calling swap:a = ten, b = 20Char values before calling Swap:c = I, d = VInt values after calling swap:a = b = 10Char values after calling Swap:c = V, d = I
Generic (Generic) delegate
You can define generic delegates through type parameters. For example:
Delegate T numberchanger<t> (t N);
The following example demonstrates the use of a delegate:
Using system;using system.collections.generic;delegate t numberchanger<t> (t N); namespace genericdelegateappl{ class Testdelegate { static int num = ten; public static int addnum (int p) { num + = p; return num; } public static int multnum (int q) { num *= q; return num; } public static int Getnum () { return num; } static void Main (string[] args) { //Create delegate instance numberchanger<int> nc1 = new numberchanger<int> (addnum); numberchanger<int> NC2 = new numberchanger<int> (multnum); Method Nc1 is invoked using a delegate object ; Console.WriteLine ("Value of Num: {0}", Getnum ()); NC2 (5); Console.WriteLine ("Value of Num: {0}", Getnum ()); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Value of Num:35value of num:175
The above is the "C # Tutorial" C # Generic (Generic) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!