一 泛型
泛型提供了一种更优雅的方式,可以让多个类型共享一组代码。泛型允许我们声明类型参数化的代码,可以用不同的类型进行实例化。
也就是说,我们可以用"类型占位符"来写代码,然后在创建类的实例时指明真实的类型。
C#提供了5种泛型:类、结构、接口、委托和方法。
二 泛型类
如下代码声明了一个SomeClass的泛型类,类型参数T1和T2列在尖括号中,当作真实类型在声明的主体中使用。
class Program
{
static void Main(string[] args)
{
//使用泛型类声明对象,提供short、int类型实参。
SomeClass<short, int> s1Class = new SomeClass<short, int>();
SomeClass<int, long> s2Class = new SomeClass<int, long>();
}
}
class SomeClass<T1, T2> where T1 : new() where T2 : new()
{
public T1 SomeVar = new T1();
public T2 OtherVar = new T2();
}
where是对泛型添加的约束,where T :new() 表示 类型T必须具有无参数的公共构造函数。
以下为可添加的约束类型:
约束 | 说明 |
T:struct | 类型参数必须是值类型。 |
T:class | 类型参数必须是引用类型;这一点也适用于任何类、接口、委托或数组类型。 |
T:new() | 类型参数必须具有无参数的公共构造函数。 |
T:<基类> | 类型参数必须是指定的基类或派生自指定的基类(非密封类) |
T:<接口> | 类型参数必须是指定的接口或实现指定的接口。 可以指定多个接口约束。 约束接口也可以是泛型的。 |
T:U | 为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。 |
三 泛型方法
泛型方法具有类型参数列表和可选的约束。
// <T1,T2> : 为泛型参数列表
// (T1 t1, T2 t2) : 为方法参数列表
// where T1 : Person : 为约束子句,表示T1类必须为Person类或其子类
public void PrintData<T1, T2>(T1 t1, T2 t2) where T1 : Person
{
...
}
调用泛型方法,应该在方法调用时提供类型实参。
下列是泛型方法的使用实例:
static public void ReverseAndPrint<T>(T[] arr)
{
Array.Reverse(arr);
foreach (var item in arr)
{
Console.WriteLine(item);
}
}
static void Main(string[] args)
{
var intArr = new int[] { 1, 2, 3, 4, 5 };
var strArr = new string[] { "a", "b", "c", "d", "e" };
ReverseAndPrint<int>(intArr); //调用泛型方法
ReverseAndPrint(intArr); //推断类型并调用
ReverseAndPrint<string>(strArr);
ReverseAndPrint(strArr);
}
四 泛型结构
与泛型类类似,泛型结构也可以有类型参数和约束。泛型结构的规则与条件与泛型类是一样的。
五 泛型委托
泛型委托和非泛型委托非常类似,不过类型参数决定了能接受什么样的方法。
// <T, R> : 为类型参数
// (T value) : 为委托形参
// R : 返回类型
delegate R MyDelegate<T, R>(T value);
下面是一个泛型委托的示例:
delegate void MyDelegate<T>(T value); //泛型委托
class Simple
{
public static void PrintString(string s)
{
Console.WriteLine(s);
}
public static void PrintUpperString(string s)
{
Console.WriteLine(s.ToUpper());
}
}
static void Main(string[] args)
{
var myDel = new MyDelegate<string>(Simple.PrintString);
myDel += Simple.PrintUpperString;
myDel("hello world");
}
六 泛型接口
泛型接口允许我们编写参数和接口成员返回类型是泛型类型参数的接口。
下面是泛型接口的示例:
interface IMyIfc<T>
{
T ReturnIt(T inValue);
}
class Simple : IMyIfc<int>, IMyIfc<string>
{
public int ReturnIt(int inValue)
{
return inValue;
}
public string ReturnIt(string inValue)
{
return inValue;
}
}
class Program
{
static void Main(string[] args)
{
var sim = new Simple();
Console.WriteLine(sim.ReturnIt(5));
Console.WriteLine(sim.ReturnIt("hello world")) ;
}
}