C# where子句
where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量。
public class MyGenericClass<T>where T:IComparable { }
class MyClassy<T, U>
{ }
public class MyGenericClass <T> where T: IComparable, new
() new() 约束出现在 where 子句的最后。
{ }
interface
MyI { }
class Dictionary<TKey,TVal> where TKey: IComparable, IEnumerable where TVal: MyI { } 5.还可以将约束附加到泛型方法的类型参数,例如:
public bool MyMethod<T>
(T t) where T : IMyInterface { }
请注意,对于委托和方法两者来说,描述类型参数约束的语法是一样的:
delegate T MyDelegate<T>() where T :new()
|