Using enums in generic methods in C#

c#, enums, generics, methods

Solution

There are a couple of different issues with that particular code snippet, some of them easier to deal with than others. A few of them are simply showing your lack of experience:

- `enum` and `Enum` are not interchangable; one is a class name and one is a C# keyword. In some cases (say, `string` and `String`) the keyword is just an alias for the type, but `enum` is not a type name, it is used to define type names. To call class methods you need to use `Enum`.

- You are trying to call the generic `TryParse<>` method with the wrong syntax; it should be `Enum.TryParse<T>`.

- Enumerations and integers are not the same type, and you cannot just compare them. They are, however, convertible to one another via an explicit typecast. Since `sizeof` returns an `int`, you need such a typecast (but see below).

The more complex issues with your code sample:

- `sizeof(enum)` doesn't do quite what you expect, I think; it returns the size in bytes of the enum, which is typically going to be 4. You probably want the `IsDefined` method, which lets you know if a particular integer value is defined in an enumerated type

- As the compiler will tell you, you can only use generic types in this context that are non-nullable. The way to define this is with a `where T: struct` constraint on your generic type. Note that, despite its name, this doesn't force your type to be a structure; it simply forces it to be a value type.

- There is no way for you specify a constraint on the generic type that it must be an enumerated type; if you pass some other value type into the method, it will throw an exception at run-time. It's up to you to handle this case properly.

Here's a working (as in -- it compiles -- I haven't actually tested it) version of the code snippet you want. However, I will point out that every one of the problems in your original code would be worked out just by reading and understanding the error messages; this is one of the most important skills you should get good at as a C# developer.

static void ReadMenuInput<T>(out T menuInput) where T : struct
{           
  while (true)
  {
    if (Enum.TryParse<T>(Console.ReadLine(), out menuInput)
     && Enum.IsDefined(typeof(T), menuInput))
    {
      break;
    }

    Console.WriteLine("Please enter a valid input.");
  } 
}

Problem

I just started learning C# today, and I am trying to make a generic method which can operate on different enums. I got some code that I found in another question on paper, but the compiler keeps complaining. I was wondering if someone could give me a hand. Here is what I have so far: ``` static void ReadMenuInput<T>(out T menuInput) { while (true) { if (enum<T>.TryParse(Console.ReadLine(), out menuInput) && menuInput < sizeof(T)) { break; } Console.WriteLine("Please enter a valid input."); } } ``` Thank you for your help!

Original source