I've seen some code https://stackoverflow.com/a/479417/1221410
It is designed to take an enum value and get the description. The relevant part is:
public static string GetDescription<T>(this T enumerationValue) where T : struct
I'm using the same code but what has confused me is I can't change the constraint
public static string GetDescription<T>(this T enumerationValue) where T : enum
public static string GetDescription<T>(this T enumerationValue) where T : class
2 issue have arisen from this.
The first is when I use the enum
as the constraint parameter. This should constrain the generic parameter to an enum. If so, why can't I type code like
var a = "hello world";
within the function. That has nothing to do with the parameter...
My second question is, when I change the constraint to class
, then the above code snippet (var a = "hello world";
) works fine but when I call the function I get the error message
'Enum.Result' must be a reference type in order to use it as parameter 'T' in the generic type or method 'GetDescription<T>(T)'
Enum.Result must be a reference type
... I thought a class was a reference type...
Why does the example at https://stackoverflow.com/a/479417/1221410 only work with struct
?