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 -

jsf - How to order SelectItems in custom renderer extending MyFaces HtmlMenuRenderer -

sql server - SQL, get max id of column -