where(泛型类型约束)
public class MyGenericClass<T> where T:IComparable { }
![]() |
---|
|
class MyClass<T, U> where T : class where U : struct { }
public class MyGenericClass<T> where T : IComparable, new() { // The following line is not possible without new() constraint: T item = new T(); }
interface IMyInterface { } class Dictionary<TKey, TVal> where TKey : IComparable, IEnumerable where TVal : IMyInterface { public void Add(TKey key, TVal val) { } }
public bool MyMethod<T>(T t) where T : IMyInterface { }
delegate T MyDelegate<T>() where T : new()
C# 语言规范
有关更多信息,请参见C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。