If I have a generic interface with a struct constraint like this:
public interface IStruct<T> where T : struct { }
I can supply an enumeration as my type T like so, because an enum satisfies a struct constraint:
public class EnumIsAStruct : IStruct<DateTimeKind> { }
C# 7.3 added an Enum constraint. The following code, which was previously illegal, now compiles:
public class MCVE<T> : IStruct<T> where T : struct, Enum { }
However, to my surprise, the following fails to compile:
public class MCVE<T> : IStruct<T> where T : Enum { }
...with the error
CS0453 The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'IStruct'
Why is this? I would expect a generic type constrained by Enum to be usable as a type argument where the type is constrained by struct but this doesn't seem to be the case - I am having to change my Enum constraint to struct, Enum. Is my expectation wrong?
Enumconstraint in your answer is interesting.Enum, but you can't do anything with it! You can't cast it to 'int', you can't combine flags with|. These all give errors:TEnum val; var i = (int)val; //ERROR: "Cannot conver type 'TEnum' to 'int'"orTEnum a, b; var c = a | b; //ERROR: "Operator '|' cannot be applied to type 'TEnum' and 'TEnum'."Useless.