Conversion of C # enum,int,string to each other

Source: Internet
Author: User

The original: C # enum,int,string conversion to each other

"Conversion of C # enum,int,string to each other "

1, enum-->string

(1) using the Object.ToString () method: The value of Colors.Green.ToString() is the "Green" string, and (2) The static method of using the enum GetName and GetNames:   public static string GetName (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 all "Blue"         values Enum.getnames (typeof (Colors)) Returns an array of enumeration strings.

2, String-->enum

(1) static method of using Enum parse: public   static Object parse (Type enumtype,string value)   For example:(Colors) Enum.parse ( typeof (Colors), "Red")

3, Enum-->int

(1) Because the base type of the enumeration is an integral type other than Char, it is possible to cast.   For example:(int) colors.red, (byte) colors.green

4, Int-->enum

(1) You can cast an integral type to an enumeration type.   For example: Colors color = (Colors) 2, then color is colors.blue (2) using the static method of the enum Toobject. Public   static Object toobject (Type enumtype,int value)   (Colors) Enum.toobject (typeof (Colors), 2), then color is Colors.blue

5, determine whether an integral type is defined in the enumeration method: Enum.isdefined

public static bool IsDefined (Type enumtype,object value) For example:enum.isdefined (typeof (Colors), n))

Reference:

1, Http://msdn.microsoft.com/zh-cn/library/system.enum (v=vs.110). aspx

2, http://www.cnblogs.com/myx/archive/2011/06/17/Enum-Int-String.html

Conversion of C # enum,int,string to 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.