C # examples of enumeration conversion, numeric or String Conversion

Source: Internet
Author: User

Examples of enumeration conversion, numbers or strings to enumeration, This article focuses on examples of C # enumeration usage, number conversion to enumeration, enumeration to numbers and their enumerated value judgment, the following is an example:

 

String Conversion to enumeration: dayofweek week = (dayofweek) enum. parse (typeof (dayofweek), "Friday"); number to enumeration: dayofweek week = (dayofweek) 5; // Friday example: Define enumeration: public Enum displaytype {All = 10, up = 20, down = 30}

 

1. numeric conversion (1) characters into enumerative string STR = "up"; displaytype = (displaytype) system. enum. parse (typeof (displaytype), STR, true); response. write (displaytype. tostring ());

The result is: The up enum. parse method has 3rd parameters. If the value is true, Case sensitivity is ignored; otherwise, Case sensitivity is considered.

(2) convert the number to int I = 30; displaytype = (displaytype) system. enum. parse (typeof (displaytype), I. tostring (); response. write (displaytype. tostring (); the result is: Down (3) enumeration is converted to displaytype = displaytype. down; string STR = displaytype. tostring (); response. write (STR); the result is: Down

(4) convert enumeration to numeric method 1: displaytype = displaytype. down; int I = convert. toint32 (displaytype. tostring ("D"); response. write (I. tostring ());

Or: (INT) enum. parse (typrof (displaytype), "down") the result is: 30

Method 2: displaytype = displaytype. down; int I = (iconvertible) (system. enum) displaytype )). toint32 (null); response. write (I. tostring (); Result: 30

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.