Allow Generic Static Classes with Extension Methods in C# #8772
Unanswered
jjaskulowski-configit
asked this question in
Language Ideas
Replies: 1 comment
-
We're getting a total overhaul of extensions in an upcoming C# version; We should consider the legacy extensions methods de-facto deprecated. I recommend looking into that new feature and voice your opinion on that matter instead! :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Overview:
Currently, C# restricts extension methods to be defined only within static non-generic classes. While methods themselves can be generic, the containing static class cannot have generic type parameters. This limitation leads to redundant and verbose code when working with complex generic types, especially in scenarios where the extension methods could benefit from class-level generics.
Proposal:
Allow static generic classes to contain extension methods. The compiler should be able to infer the generic type parameters of the static class from the type of the first parameter of the extension method (the this parameter) during method invocation. This change would enable developers to write more concise and maintainable code by eliminating the need to explicitly specify class-level generic type arguments that can be inferred.
Example:
Consider the following code that is currently not allowed in C#:
In this example:
RegistrationExtensions
is a static generic class.WithNamedDependency
is an extension method defined within this class.The method operates on
IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle>
.Current Limitation:
Since C# does not allow extension methods within generic static classes, developers must work around this limitation by:
Moving generic type parameters to the method level, leading to repetitive and less readable code.
Explicitly specifying all generic type arguments when invoking the method, even when some can be inferred.
Proposed Change:
Syntax Allowance: Permit extension methods to be declared within static generic classes.
Type Inference: Enable the compiler to infer the generic type parameters of the static class based on the type of the first argument (the extended type) during method invocation.
Usage with the Proposed Change:
With the proposed enhancement, the method can be invoked succinctly:
registration.WithNamedDependency<TDependency>("someName");
Here:
registration is an instance of
IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle>
.The compiler infers
TLimit
,TReflectionActivatorData
, andTStyle
from registration.Only
TDependency
needs to be specified if it cannot be inferred.Benefits:
Reduced Redundancy: Eliminates the need to specify class-level generic type arguments explicitly when they can be inferred.
Improved Readability: Results in cleaner and more maintainable code.
Enhanced Flexibility: Allows for more powerful and reusable extension methods that leverage class-level generics.
Potential Challenges:
Compiler Implementation: Requires updates to the compiler's type inference mechanisms to support inferring class-level generics from method parameters.
Language Consistency: Care must be taken to ensure that this change aligns with existing language design principles and does not introduce ambiguity.
Conclusion:
By allowing static generic classes to contain extension methods with inferred generic type parameters, C# can provide developers with a more expressive and efficient way to write generic code. This enhancement would align with the language's goals of reducing boilerplate and improving developer productivity.
Beta Was this translation helpful? Give feedback.
All reactions