C
C#2y 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
ero2y ago
no
Hugh
Hugh2y ago
ok
ero
ero2y ago
ask #roslyn if you really wanna know why
Hugh
Hugh2y ago
I get that it isn't something you can do in other languages - I was just wondering if it were different here 🙂
333fred
333fred2y ago
You can do it in other languages using associated types. Maybe we'll get them someday
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server
More Posts
❔ Can I use slice operator with loop index (remainder operator%)?I want to slice an array from start to end index but if end index exceeds the array length, loop it ❔ My ASP.NET project isn't launchingI just made templates for a Standalone Angular project and a ASP.NET 6 project but it seems like onl❔ Making properties accessible by both static and non-static methodsI had to make a static duplicate of a property, because I wanted it to be accessible by both non-sta❔ Windows Forms different controls but same event doing different code?```cs private void OnSecondsChange(object sender, EventArgs e) { textBoxUpTime.Text += $"{sender✅ What is better, in this case, Replace() or Substring()In Unity it seems that text input comes with a 'Zero Width Space Character' at the end, and that mes❔ How to pass anchor tag values to controller to render view based on selected valueHI, how do I pass the values assigned to an anchor tag to the controller so that the controller can ❔ How to force method call regardless of type?```cs public class TempBaseClass { } public class TempGenericClass<T> : TempBaseClass { public ❔ Double property with [Required] data annotation does not validateHow can I make it so that the req body has to have a DiscountPercentage field?❔ 'Type may be weakened' inspection in riderIn intelliJ there is an inspection *look at screenshot* for java. Is there an equivalent for it in r❔ Where to learn building APIs ?hey guys can anyone tell me where i can learn building APIs with C#