ASSIGNING ENUM IN C# WITH A STRING

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C# 
C#
 1183  View(s)
Ratings:
Rate this:

I was wondering if there's an option to sign an enum with the options of white black and red, directly with a string "red". without using if's... I mean i don't want to write:

if (string1=="red") {mycolor=Color.red};

thanks!

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Assigning enum in c# with a string

    hi Tanuj,

    Color ColorEnum = (Color)Enum.Parse(typeof(Color), "YourValue");

    or

    Color ColorEnum = (Color)Enum.Parse(typeof(Color), "YourValue",true);

    This Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!