Issue with nullable Enum in generic method #6210
-
There appears to be a bug in the compiler when passing a nullable enum type with contraint of Enum: `
} var result = GetFlag(nullable, FlagsEnum.Flag1); CS0312 The type 'Test.FlagsEnum?' cannot be used as type parameter 'TEnum' in the generic type or method 'Program.GetFlag(TEnum?, TEnum)'. The nullable type 'Test.FlagsEnum?' does not satisfy the constraint of 'System.Enum'. Seems to be an incorrect compile error. NET 6.0 / VS 17.2.0 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
You need to constrain static bool GetFlag<TEnum>(TEnum? field, TEnum flag)
where TEnum : struct, Enum
=> field?.HasFlag(flag) == true; |
Beta Was this translation helpful? Give feedback.
-
Same issue as in #6004. |
Beta Was this translation helpful? Give feedback.
You need to constrain
TEnum
tostruct
. (SharpLab)