简体   繁体   中英

C# unbounded generic type as constrain

Is it possible to have a generic constraint which is an unbounded generic type?

For example:

public T DoSomething<T>(T dictionary) where T : IDictionary<,>
{
    ...
}

Edit: Just to explain the context, I want to constrain the usage of the method to an IDictionary, but for the method itself it does not matter exactly what TKey and TValue are.

这是不可能的,您需要指定类型参数:

public T DoSomething<T, TKey, TValue>(T dictionary) where T : IDictionary<TKey, TValue> { ... }

There is an issue in rosylin github , however, it's not done yet. So it's not possible right now, but I expect it will be relatively soon.

No, you can't do that. Maybe you can use

public IDictionary<TKey, TValue> DoSomething<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
{
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM