Reputation: 89
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!
Upvotes: 3
Views: 1949
Reputation: 28338
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
.TryParse<>
method with the wrong syntax; it should be Enum.TryParse<T>
.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 typewhere 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.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.");
}
}
Upvotes: 7