ALLINSIGHT

Home of the AlmostImplementedException

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.

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.

To get a string from an enum, just call ToString

Share :

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *