C # enum,int,string Conversion of reciprocal conversions enumeration

Source: Internet
Author: User

An enum provides a base class for an enumeration whose underlying type can be any integral type except Char. If you do not explicitly declare an underlying type, use Int32. Programming languages typically provide syntax to declare enumerations that consist of a set of named constants and their values. Note: The base type of an enumeration type is any integral type except Char, so the value of the enumeration type is an integer value. The Enum provides some useful static methods: (1) to compare the methods of enumerating instances of the class (2) to convert the value of the instance to its string representation (3A method that converts the string representation of a number to an instance of this class (4creates a method that specifies an instance of an enumeration and a value. Example:enumColors {Red, Green, Blue, Yellow}; Enum-String (1) using the Object.ToString () method: Values such as Colors.Green.ToString () are"Green"string; (2GetName and GetNames using the static method of enum: Public Static stringGetName (Type enumtype,object value) Public Static string[] GetNames (Type enumtype) For example: Enum.getname (typeof(Colors),3)) and Enum.getname (typeof(Colors), Colors.blue)) are the values of"Blue"Enum.getnames (typeof(Colors)) will return an array of enumerated strings. String-Enum (1using the static method of Enum, parse: Public StaticObject Parse (Type enumtype,stringvalue) For example: (Colors) Enum.parse (typeof(Colors),"Red") Enum-Int (1because the base type of the enumeration is an integral type other than Char, you can cast it. For example: (int) Colors.red, (byte) Colors.greenint-Enum (1you can cast an integral type to an enumeration type. Example: Colors color= (Colors)2, then color is Colors.blue (2) using the static method of the enum Toobject.  Public StaticObject Toobject (Type enumtype,intvalue) For example: Colors color= (Colors) enum.toobject (typeof(Colors),2, then color is the method by which Colors.blue determines whether an integer is defined in an enumeration: Enum.isdefined Public Static BOOLIsDefined (Type enumtype,object value) For example: enum.isdefined (typeof(Colors), N))

C # enum,int,string Conversion of reciprocal conversions enumeration

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.