Examples of enumeration conversion, numbers or strings to enumeration, This article focuses on examples of C # enumeration usage, number conversion to enumeration, enumeration to numbers and their enumerated value judgment, the following is an example:
String Conversion to enumeration: dayofweek week = (dayofweek) enum. parse (typeof (dayofweek), "Friday"); number to enumeration: dayofweek week = (dayofweek) 5; // Friday example: Define enumeration: public Enum displaytype {All = 10, up = 20, down = 30}
1. numeric conversion (1) characters into enumerative string STR = "up"; displaytype = (displaytype) system. enum. parse (typeof (displaytype), STR, true); response. write (displaytype. tostring ());
The result is: The up enum. parse method has 3rd parameters. If the value is true, Case sensitivity is ignored; otherwise, Case sensitivity is considered.
(2) convert the number to int I = 30; displaytype = (displaytype) system. enum. parse (typeof (displaytype), I. tostring (); response. write (displaytype. tostring (); the result is: Down (3) enumeration is converted to displaytype = displaytype. down; string STR = displaytype. tostring (); response. write (STR); the result is: Down
(4) convert enumeration to numeric method 1: displaytype = displaytype. down; int I = convert. toint32 (displaytype. tostring ("D"); response. write (I. tostring ());
Or: (INT) enum. parse (typrof (displaytype), "down") the result is: 30
Method 2: displaytype = displaytype. down; int I = (iconvertible) (system. enum) displaytype )). toint32 (null); response. write (I. tostring (); Result: 30