Click here to Skip to main content
Click here to Skip to main content

A Generic enum Parser in C#

By , 22 May 2011
 
Generic enum Parser
 
There are many ways to parse an enum . For example, we could use switch in C# to parse enum. But the problem will be if that enum has 10 items in it (for example), then we need to write 10 case statements. So if we have 10 enums with 10 items, then we need to write 10x10 case statements plus all the deafult statements.
 
I use the following technique to parse enum. First of all, I use the following two enums to describe this tip:
 
public enum EnumOne
{
     None = 0,
     One = 1,
     Two = 2,
     Three = 3
}
 
public enum EnumTwo
{
     None,
     One,
     Two,
     Three
}
 
So we could parse the above enum using the following switch code block,
 
public EnumOne ParseEnum(string item)
{
     switch (item)
     {
       case "One":
         return EnumOne.One;
       case "Two":
         return EnumOne.Two;
       case "Three":
         return EnumOne.Three;
     }
     return EnumOne.None;
}
 
But we want to parse EnumOne and EnumTwo. As a result, we need to write two blocks of switch, whereas the following generic enum parser will do the job using minimum lines of code.
 
public TEnum ParseEnum<TEnum>(string item, bool ignorecase = default(bool))
            where TEnum : struct
{
    TEnum tenumResult = default(TEnum);
    return Enum.TryParse<TEnum>(item, ignorecase, out tenumResult) ?
           tenumResult : default(TEnum);
}
 
To test the above code, we could use the following code block:
 
public void Test()
{
     Console.WriteLine("{0}, {1}, {2}, {3}, {4}",
           new object[]
           {
              ParseEnum("One"),
              ParseEnum<EnumOne>("Two"),
              ParseEnum<EnumTwo>("TwoTwo"),
              ParseEnum<EnumTwo>("1"),
              ParseEnum<EnumTwo>(string.Empty)
          });
}
 
In this tip, we assume that whatever enum we are going to parse, it has a default value for example, in here None is default for EnumOne and EnumTwo, otherwise parser will return first item of the enum.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Member
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiona simpler methodmemberMickey Perlstein25 Jul '12 - 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 22 May 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid