C
C#3y ago
Hugh

❔ Generics with different input and output type

I'm writing a generic at the moment which it something like this:
T GetSomething<T, U>(U InputValues)
T GetSomething<T, U>(U InputValues)
When I'm calling it, is there any way to only specify T without specifying U (as this can be inferred from the argument being passed in). Ideally I'd like to be able to type something like:
MyInputType inputValues;
MyType result = GetSomething<MyType>(inputValues);
MyInputType inputValues;
MyType result = GetSomething<MyType>(inputValues);
rather than
MyInputType inputValues;
MyType result = GetSomething<MyType, MyInputType>(inputValues);
MyInputType inputValues;
MyType result = GetSomething<MyType, MyInputType>(inputValues);
Is something like this possible at all?
6 Replies
ero
ero3y ago
no
Hugh
HughOP3y ago
ok
ero
ero3y ago
ask #roslyn if you really wanna know why
Hugh
HughOP3y ago
I get that it isn't something you can do in other languages - I was just wondering if it were different here 🙂
333fred
333fred3y ago
You can do it in other languages using associated types. Maybe we'll get them someday
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?