C # enum, int, string three types convert each other

Source: Internet
Author: User

The reciprocal # code between enum, int, string three types:
1  Public enumSex2 { 3man=1,4woman=25 }6 7  Public Static voidEnumconvert ()8 {9     intSexnum = (int) Sex.man;TenConsole.WriteLine ("to convert an enumeration to an integer:"+sexnum);//1 One  A     stringSexstr =Sex.Woman.ToString (); -Console.WriteLine ("to convert an enumeration to a string:"+sexstr);//"Woman" -  theSex sexstrenum = (sex) enum.parse (typeof(Sex),"Mans"); -Console.WriteLine ("to convert a string to an enumeration:"+sexstrenum.tostring ());//"Man" -  -Sex sexnumenum = (sex)1; +Console.WriteLine ("converts an integer to an enumeration:"+sexnumenum.tostring ());//"Man" -  +     stringNumtostr = Enum.getname (typeof(Sex),2); AConsole.WriteLine ("convert an integer to a string:"+numtostr);//"Woman" at  -     intStrtonum = (int) Enum.parse (typeof(Sex),"Woman"); -Console.WriteLine ("Convert a string to an integer:"+strtonum);//2 -}

#结果:

Willingtolove

————————————————***

C # enum, int, string three types convert to and from each other

Related Article

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.