C # enumeration, string, and value conversion Using System;
Class Program
{
Public Enum Color
{
Red = 0xff0000 ,
Orange = 0xffa500 ,
Yellow = 0xffff00 ,
Lime = 0x00ff00 ,
Cyan = 0x00ffff ,
Blue = 0x0000ff ,
Purple = Zero X 800080
}
Static Void Main ( String [] ARGs)
{
Color color = Color. blue;
String Colorstring = " Blue " ;
Int Colorvalue = 0x0000ff ;
//Enumerated to string
StringEnumstringone=Color. tostring ();
StringEnumstringtwo=Enum. getname (Typeof(Color), color );
// enumeration to value
int enumvalueone = color. gethashcode ();
int enumvaluetwo = ( int ) color;
int enumvaluethree = convert. toint32 (color);
// string to enumeration
color enumone = (color) enum. parse ( typeof (color ), colorstring);
// String Conversion value
int enumvaluefour = ( int ) enum. parse ( typeof (color ), colorstring);
// value conversion enumeration
color enumtwo = (color) colorvalue;
color enumthree = (color) enum. toobject ( typeof (color ), colorvalue);
//Conversion string
StringEnumstringthree=Enum. getname (Typeof(Color), colorvalue );
}
}
// Assume that the enumerated values are as follows:
Public Enum Dbprovidertype
{
Sqlserver,
Oracle
}
// 1. Convert enumeration to a string:
String Strdbtype = Dbprovidertype. sqlserver. tostring ();
// 2. Convert string to enumeration:
dbprovidertype dbtype = (dbprovidertype) enum. parse ( typeof (dbprovidertype ), strdbtype, true );