Add System.Enum<TUnderlying> and allow it as a generic constraint for enum types #9540
Unanswered
CodeSetting
asked this question in
Language Ideas
Replies: 2 comments 6 replies
-
I think if you add |
Beta Was this translation helpful? Give feedback.
3 replies
-
Is this even possible without a recompilation of literally everything? Because right now, every enum inherits from |
Beta Was this translation helpful? Give feedback.
3 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.
Uh oh!
There was an error while loading. Please reload this page.
-
While the addition of
where T : Enum
was a most welcome enhancement to the C# language, it could be more powerful.If the simulated inheritance where more like
enum : T
⟶System.Enum<T> where T : IBinaryInteger<T>
⟶Enum
, this would support some more powerful possibilities.Currently, if I want to have a generic method that acts on some values shared between multiple types of
enum
, I'm forced to incur boxing overhead that is not strictly necessary.Consider the following
enum
types:when directly accessed, no boxing (via
object
) is required:now, consider the case where I want to work with generic constraints, while I can do the following, the boxing (via
object
) of(int)(object)theEnum
is less than ideal.With my proposed solution, in the following (
(int)theEnum == (int)SharedEnum.SharedGood)
), it might be possible to avoid the intermediate boxing to(object)
, and it would more closely resemble the direct access.It might also allow
enum
to more broadly participate in the features provided by IBinaryInteger. Although, extending this behavior to include these additional features might introduce unintended consequences.Thank you for taking the time to read this and for your consideration.
Beta Was this translation helpful? Give feedback.
All reactions