To constrain a generic type parameter to be of an enum type, I previously constrained them like this, which was the best I could go for constraining type T for enums in pre-C# 7.3:
void DoSomething<T>() where T : struct, IComparable, IConvertible, IFormattable
Now, C# 7.3 adds a new feature to constrain a generic type to System.Enum
.
I tried using the enum constraint with the VS2017 15.7 update released today, and it compiles successfully when I write it like this (given I have a using System;
directive):
void DoSomething<T>() where T : Enum
However, using the enum
keyword does not work and causes the compiler to throw the following errors (there are more errors following, expecting a method body, but not really worth mentioning here I guess):
void DoSomething<T>() where T : enum
^ error CS1031: Type expected
error CS1002: ; expected
^ error CS1001: Identifier expected
error CS1514: { expected
error CS1513: } expected
Since there is a struct
constraint working for structures, I do not understand why enum
doesn't work here for enums. It's true that enum
does not map to an actual type like int
would do for Int32
, but I thought it should behave the same as the struct
constraint.
Did I just fall into an experimental feature trap not being fully implemented yet, or was this done on purpose in the specification (why?)?