Ami*_*iri 5 c# generics generic-constraints
是否可以使用通用约束,这是一种无界泛型类型?
例如:
public T DoSomething<T>(T dictionary) where T : IDictionary<,>
{
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:为了解释上下文,我想将方法的用法限制为IDictionary,但对于方法本身而言,TKey和TValue究竟是什么并不重要.
这是不可能的,您需要指定类型参数:
public T DoSomething<T, TKey, TValue>(T dictionary) where T : IDictionary<TKey, TValue> { ... }
Run Code Online (Sandbox Code Playgroud)