c# - Enums: Retrieve enum value from enumeration name string -


this question has answer here:

the enum:

public enum enumname {     gary = 1,     dave = 2 } 

the input:

string inputvalue = "gary"; 

what best way retrieve value enum string? i.e. return value 1.

you can use enum.parse convert string enum, or enum.tryparse if less sure of input.

(enumname)enum.parse(typeof(enumname), inputvalue) 

you can convert enum underlying type, default int if unspecified, casting. :

(int)enum.parse(typeof(enumname), inputvalue) 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -