Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 795
Amir Abiri Avatar asked Aug 01 '12 18:08

Amir Abiri


2 Answers

This isn't possible, you need to specify the type parameters:

public T DoSomething<T, TKey, TValue>(T dictionary) where T : IDictionary<TKey, TValue> { ... }
like image 151
Lee Avatar answered Sep 19 '22 19:09

Lee


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.

like image 22
Alex Zhukovskiy Avatar answered Sep 21 '22 19:09

Alex Zhukovskiy