Generics 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, when declaring a class or method, because you do not know what type of argument the user is going to pass in, "Dig a Hole (" <T> ") in the place of the incoming type, and use it when we fill it with the specific data type.
Generic class:
We define a class based on the previous knowledge:
Class Data {public int n_data; }
At this point the data type of n_data has been determined to be of type int, so assigning a value to him can only be of type int, if overridden to the following generic class:
Class data<t> {public T n_data; }
At this point the data type of n_data is not sure what type, so for him to assign the value of the need to specify T is the type of n_data, that is, pits,
data<int> data = new data<int> (); Specify T as int data<string> Data = new data<string> (); Specify T As String
Of course, the above example is not able to specify the T array, if you want to let the N_data type array, the following example can be satisfied:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Example { class data<t>//generic class {public t[] n_data;//generic variable public Data (int size)// constructor method, new when calling the construction method to open space { n_data = new t[size]; } Enter public void SetData (int index,t value) { N_data[index] = value; } Output public T GetData (int x) { return n_data[x]; } } Class program { static void Main (string[] args) { data<int> Data = new data<int> (5); C28/>DATA.N_DATA[2] = 2; Console.WriteLine (Data.n_data[2]);}}
The result is: 2
Generic methods:
Let's take the swap exchange as an example, in C + + the Swap function is written in this way:
#include <iostream> using namespace std; Template <typename t> void Swap1 (T &a,t &b)//can also be considered generic { T temp; temp = A; A = b; b = temp; } int main () { int a=0,b=1; Swap1 (A, b); cout<<a<< "\ t" <<b<<endl; return 0; }
Results: 1 0
If A and B are character types, the above functions also apply. The C#swap method is as follows:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Example { class data {//swap method, ref is pass by address public static void swap<t> (Ref t A, ref T B)
{ T temp; temp = A; A = b; b = temp; } static void Main (string[] args) { string a = "HC"; String b = "666"; Swap (ref a,ref B); Console.WriteLine (A + "\ T" +b); Result 666 HC } } }
Result: 666 HC This is very similar to C + +.
Generic delegate:
The delegate also has generics, followed by the example above:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Example {public delegate void mydelegate<t> ();//Generic delegate class Data<t> {Priva Te T A; Private T B; public void SetValue (T x, t y) {a = x; b = y; }//swap method, ref is passing public void swap () {T temp by address; temp = A; A = b; b = temp; } public void Printvalue () {Console.WriteLine (a + "\ T" + B); }} class Program {static void Main (string[] args) {data<string& Gt data = new data<string> (); Data.setvalue ("HC", "666"); mydelegate<string> my = new mydelegate<string> (DATA.SWAP); my + = Data.printvalue; My (); Result 666 HC} } }
Results:
About generics on the introduction so much, and what wrong place to welcome point ^_^
The above is the C # Learning Diary---Generic class, generic method, generic delegate content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!