Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use generic Tryparse with Enum?

Tags:

c#

enums

generics

I'm trying to build generic function that get from user string and try to parse it to Enum valuse like this:

private Enum getEnumStringEnumType(Type i_EnumType)     {         string userInputString = string.Empty;         Enum resultInputType;         bool enumParseResult = false;          while (!enumParseResult)         {                             userInputString = System.Console.ReadLine();             enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);         }     } 

But i get:

The type 'System.Enum' must be a non-nullable value type in order to use it as parameter 'TEnum' in the generic type or method 'System.Enum.TryParse<TEnum>(string, bool, out TEnum)    . 

The Error means that i need to decalare a specific Enum for resultInputType? How can I fix this ? Thanks.

like image 226
Rami Avatar asked May 21 '12 13:05

Rami


People also ask

What does enum TryParse do?

TryParse(Type, String, Object)Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

What is TEnum C#?

TEnum is the Generic type of enumeration. You can pass any of your enumeration to that method. The second method is a non-generic one, where you would use a typeof keyword to identify the enums and return the enum names as a string collection.


2 Answers

The TryParse method has the following signature:

TryParse<TEnum>(string value, bool ignoreCase, out TEnum result)     where TEnum : struct 

It has a generic type parameter TEnum that must be a struct and that is used to determine the type of enumeration being parsed. When you don't provide it explicitly (as you did), it will take the type of whatever you provide as the result argument, which in your case is of type Enum (and not the type of the enumeration itself).

Note that Enum is a class (despite it inheriting from ValueType) and therefore it does not satisfy the requirement that TEnum is a struct.

You can solve this by removing the Type parameter and giving the method a generic type parameter with the same constraints (i.e. struct) as the generic type parameter on the TryParse function.

So try this, where I've named the generic type parameter TEnum:

private static TEnum GetEnumStringEnumType<TEnum>()     where TEnum : struct {     string userInputString = string.Empty;     TEnum resultInputType = default(TEnum);     bool enumParseResult = false;      while (!enumParseResult)     {                         userInputString = System.Console.ReadLine();         enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);     }     return resultInputType; } 

To call the method, use:

GetEnumStringEnumType<MyEnum>(); 
like image 86
Daniel A.A. Pelsmaeker Avatar answered Oct 07 '22 23:10

Daniel A.A. Pelsmaeker


You should make a generic method:

private T getEnumStringEnumType<T>() where T : struct, IConvertible     {         string userInputString = string.Empty;         T resultInputType = default(T);         bool enumParseResult = false;          while (!enumParseResult)         {             userInputString = System.Console.ReadLine();             enumParseResult = Enum.TryParse<T>(userInputString, out resultInputType);         }          return resultInputType;     } 

usage:

public enum myEnum { val1, val2 }  myEnum enumValue = getEnumStringEnumType<myEnum>(); 
like image 41
eyossi Avatar answered Oct 07 '22 21:10

eyossi