0

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?

Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • You are getting that error because an enum is not a class, it is a struct. – CathalMF Nov 14 '16 at 12:34
  • 2
    All enums are structs, so `where T : struct` will work with enums. `where T : enum` is simply not valid C# syntax. (but we all hope that will change some day) – Dennis_E Nov 14 '16 at 12:35
  • `where T : enum` is not a valid constraint, and all enums are structs so you can't use `where T : class`. Why do you need to change the constraint at all? – Lee Nov 14 '16 at 12:35
  • What is this `Enum.Result` and how does `var a = "hello world";` fit in with all this? – Dennis_E Nov 14 '16 at 12:35
  • @Lee, I don't *need* to, but if I don't then I won't understand or run into these type of issues... I would have continued not understanding it. By breaking stuff I can try to fix it. When I can't fix it, I can research. When research doesn't fix it, StackOverflow does :) – MyDaftQuestions Nov 14 '16 at 12:47

2 Answers2

1

The Problem why T : class won't work is because an enum always is a struct. So you see it the wrong way. You write:

Enum.Result must be a reference type... I thought a class was a reference type...

You are right that a class is a reference type. But Enum.Result isn't a class. It's a struct as mentioned above. Your constraint T : class just accept a reference type.

Further you can't type var a = "hello world"; in your function if you change constraint to T : enum, because this constraint isn't valid. So you wouldn't be able to write any valid code in your method before you fix your constraint.

Take a look at msdn for clarify which constraints are possible.

Sebi
  • 3,879
  • 2
  • 35
  • 62
  • Link to english https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters – thermite Feb 20 '18 at 17:49
1

As the comments say, enum is not a class. However, Enum is. It's just a special one and if you try to constrain the type like that, it won't let you:

public static string GetDescription<T>(this T enumerationValue) where T : Enum

Constraint cannot be special class 'Enum'

The existing code from the question you linked is about the best you can do but see here if you want more info: Create Generic method constraining T to an Enum

As to the second part of your question, about constraining it to 'class' instead, there must be something else going on there.

Edit - I'm dumb - what Sebi said. :)

There is no Enum.Result in either your code snippets or the answer you linked to so I think we'll need to see more of your code to answer that one.

Community
  • 1
  • 1
Steve
  • 1,266
  • 16
  • 37