C # enumeration, string, and value conversion

Source: Internet
Author: User
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 ;

// enumeration to string
string enumstringone = color. tostring ();
string enumstringtwo = 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:

StringStrdbtype=Dbprovidertype. sqlserver. tostring ();
//2. Convert string to enumeration:

dbprovidertype dbtype = (dbprovidertype) enum. parse ( typeof (dbprovidertype ), strdbtype, true );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.