© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
6 replies
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
T
without specifying
U
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

cannot convert from 'TypeA' to 'TypeB' with generics
C#CC# / help
2y ago
Generics and type inference
C#CC# / help
4y ago
✅ c# input and output files
C#CC# / help
3y ago
✅ input and output file help with 2d arrays
C#CC# / help
3y ago