简介:
泛型,就是在定义方法时先不声明方法的返回值类型或者参数类型,只是声明占位符,而是在调用方法时才声明类型。泛型是延迟vb.net教程声明的:即定义的时候没有
c#教程指定具体的参数类型,把参数类型的声明推迟到了调用的时候才指定参数类型。 延迟思想在程序架构设计的时候很受欢迎。
案例:
class GenericMethod
{
static void Main(string[] args)
{
int iValue = 123;
string sValue = "123";
DateTime dtValue = DateTime.Now;
Console.WriteLine("***********Generic***************");
//调用泛型方法
GenericMethod.Show<int>(iValue);
GenericMethod.Show<string>(sValue);
GenericMethod.Show<DateTime>(dtValue);
Console.ReadKey();
}
//定义泛型
public static void Show<T>(T tParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(GenericMethod), tParameter.GetType().Name, tParameter.ToString());
}
}
执行结果:
Generic****
This is lamba.GenericMethod,parameter=Int32,type=123
This is lamba.GenericMethod,parameter=String,type=123
This is lamba.GenericMethod,parameter=DateTime,type=2020/6/8 上午 09:39:34