-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: allow use of non-default constructors in generics #3244
Comments
(Not speaking for the language team, just speaking as a partial owner of the System.Reflection namespace.) This would require runtime support if we wanted to make it efficient. I have some ideas for how we could plumb this through the runtime and reflection APIs in a high-performance fashion, so in theory this should be doable. But I'm having trouble seeing how this would be viable if targeting a runtime that doesn't natively expose these efficient APIs. |
@GrabYourPitchforks Is there a possibility that this could be made available in .NET5? |
Duplicate of #769. |
@Unknown6656 The API portion would be tracked by dotnet/runtime#31895 and dotnet/runtime#23716. In theory you'd be able to use the public APIs even without language support. They're just APIs, after all. I'm not on the language team so can't speak for their plans in this area. |
This would be covered by the Shapes proposal (#164), where generic constraints should cover every type of member of a type, including constructors. |
Closing as duplicate of #769. |
Have you considered allowing a constructor prototype to be defined in the where clause of a generic. This would further constrain the type to have a constructor of the given format.
T GetFoo< T >(int i) where T : new(int i) // or just new(int)
{
return new T(5);
}
The text was updated successfully, but these errors were encountered: