Enums Constraint for Generics
Sometimes I have found my self in the need of doint something like:
where TEnum : Enum
... which currently is not possible.
Workarounds for this do exist, like:
where TEnum : struct, IConvertible
... but it could lead to bugs if not used with care.
Hope a new constraint for enums could make it for .NET 5.
Thanks for reading.

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.
- The Visual Studio Team
4 comments
-
Mojmir Rychly commented
I just would like to just share my personal opinion. The C# language has enhanced in the last years, I wonder if there will be something even more breakthrough - I would put my main vote to something related improving the Enums and the Generics - anything that brings there a special treatment. I would set the "struct" as the not well done part of the CLR. I am not the big expert of languages area and I clearly see this a quite recognizable gap, so perhaps it helps also to improve effort here. (I was actually pushed to write it spontaneously after many times this current pure generic manner with enums stopped me... :)
-
Robird Liu commented
It's useful and possible in implementation. Can't agree more.
-
Michel Bretschneider commented
How about joining forces? As there are some suggestions for enum constraints, why not vote for the one most likely to be noticed - the one with most votes so far. See: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2277087-add-in-support-for-enum-and-delegate-constraints
Maybe it's even possible to merge both suggestions without having all voters to switch their vote.
-
Matthieu Penant commented
See also http://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint for reference. That would be a nice built-in feature.