Home >Backend Development >C++ >Why Are Generic Attribute Types Not Allowed in C#?
C#'s Restriction on Generic Attributes: An Unexplained Mystery
Attempting to use generic types within C# attributes results in a baffling compile-time error. For instance:
<code class="language-csharp">[Validates<string>] public static class StringValidation { // ... }</code>
This contrasts with languages like Java, where such usage is permitted. C#, however, steadfastly prohibits generic attribute types. The precise reason for this remains unclear.
The ECMA C# 2 and 3 specifications mention this limitation without offering any explanation. A former Microsoft developer and C# language designer, Eric Lippert, suggests that there's no specific, documented rationale. The decision likely stems from a prioritization of language and compiler simplicity, focusing development efforts on features with demonstrably higher value.
Therefore, the prohibition of generic attribute types in C# stands as a noteworthy, and potentially limiting, characteristic of the language, lacking a clear and publicly available justification.
The above is the detailed content of Why Are Generic Attribute Types Not Allowed in C#?. For more information, please follow other related articles on the PHP Chinese website!