Generics allow you to defer writing the specification of a data type for a programming element within a class or method until it is actually used in your program. In other words, when declaring a class or method, because we don’t know what type of parameters the user wants to pass in, we “dig a hole (“<T>”)” where the type is passed in. When we use it, we Fill it in with specific data types.
Generic class:
Based on the knowledge we learned earlier, we define a class:
class Data { public int n_data; }
At this time, the data type of n_data has been determined to be the int type , so when assigning a value to it, it can only be of int type. If it is rewritten as the following generic class:
class Data<T> { public T n_data; }
At this time, the data type of n_data is not yet sure what type it is, so it is When he assigns a value, he needs to specify T, which is the type of n_data, that is, to fill in the pit,
Data<int> data = new Data<int>(); //指定T为int Data<string> data = new Data<string>(); //指定T为string
Of course, in the above example, T cannot be specified as an array. If you want to If the type of n_data is an array, the following example can be satisfied:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { class Data<T> //泛型类 { public T[] n_data; //泛型变量 public Data(int size) //构造方法,new的时候调用构造方法开辟空间 { n_data = new T[size]; } //输入 public void setdata(int index,T value) { n_data[index] = value; } //输出 public T getdata(int x) { return n_data[x]; } } class Program { static void Main(string[] args) { Data<int> data = new Data<int>(5); data.n_data[2] = 2; Console.WriteLine(data.n_data[2]); } } }
The result is: 2
Generic method:
We take the swap exchange method as an example. The swap function in C++ is written like this:
#include <iostream> using namespace std; template <typename T> void swap1(T &a,T &b) //也可以看作泛型 { 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; }
Result: 1 0
If a and b are character types, the above functions are also applicable. The C# swap method is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { class data {//swap方法,ref是按地址传递 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); //结果 666 HC } } }
Result: 666 HC This is very similar to C++
Generic delegate:
There are also generic delegates Type, continue the above example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { public delegate void MyDelegate<T>();//泛型委托 class Data<T> { private T a; private T b; public void setvalue(T x, T y) { a = x; b = y; } //swap方法,ref是按地址传递 public void swap() { T temp; temp = a; a = b; b = temp; } public void printvalue() { Console.WriteLine(a + "\t" + b); } } class program { static void Main(string[] args) { Data<string> data = new Data<string>(); data.setvalue("HC","666"); MyDelegate<string> my = new MyDelegate<string>(data.swap); my += data.printvalue; my(); //结果 666 HC } } }
Result: