The enum generic constraint does not function as expected. For nullability analysis, it is not even able to detect it as non-nullable by default. #9166
-
Enum is considered a reference type based on its definition and is not treated as a special value type that is non-nullable by default. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 45 replies
-
To get the most use for the constraint you can use both. public static void Show<T>() where T: struct, Enum
{
} |
Beta Was this translation helpful? Give feedback.
-
The analysis is correct. using System;
M<Enum>(); // Prints true
void M<T>() where T : Enum => Console.WriteLine(default(T) is null); |
Beta Was this translation helpful? Give feedback.
-
Perhaps it's time for a built-in analyzer for |
Beta Was this translation helpful? Give feedback.
The design is complete. An
enum
is nothing more than astruct
that inherits fromSystem.Enum
. No additional meaning would be provided by allowing you to saywhere T : enum
, it would just be another way of stating the same thing.