1 04 2013
How to convert string to enum
It is good practice to use enums to choose which part of the code should be executed, but sometimes you’ll get a string and have to convert it to an enum.
Here is, how you can do it.
First, declare a custom enum or use a given one.
1 2 3 4 5 6 7 |
enum Months { January, February, March, April } |
And then use the Enum.Parse command. This command uses 2 parameters. First the type of the enum and second the string of the enum-value.
1 |
Months m = (Months)Enum.Parse(typeof(Months), "March"); |
To get a string from an enum, just call ToString
1 |
string s = Months.January.ToString() |
Share :
Caching objects in .Net with MemoryCache How to find large tables in Sql Server