C # unlimited generic type like constrain

Is it possible to have a general constraint that is an unlimited common type?

For instance:

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

Edit: To explain the context, I want to limit the use of the method in an IDictionary, but for the method itself it does not matter what TKey and TValue are.

+5
source share
3 answers

This is not possible, you need to specify parameters like:

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

There is a problem in rosylin github , however this has not yet been done. So this is not possible now, but I expect it to be relatively soon.

+1
source

No, you cannot do this. Perhaps you can use

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

All Articles