C # enumeration to string method tutorial, c # enumeration string tutorial

Source: Internet
Author: User

C # enumeration to string method tutorial, c # enumeration string tutorial

Sometimes we need to convert enumerations to strings. How can we convert enumerations to strings?

Enumerated to string

If you want to convert enumeration to a string, you can convert it directly. See the code.

Public enum Di {////// Orbit ///Railway ,////// River ///River,} static void Main (string [] args) {Console. WriteLine (Di. Railway. ToString ());}

In this way, the enumeration can be converted to a string.

In addition to this method, you can use the C #6.0 keyword. Please refer to the code.

            Console.WriteLine(nameof(Di.Railway));
String to Enumeration

If you want to convert an enumeration to a string, how can you convert the string to an enumeration? AvailableEnum. Parse but this method can throw an exception, so you need to know that the string can be converted

Public enum Di {////// Orbit ///Railway ,////// River ///River,} static void Main (string [] args) {string str = Di. railway. toString (); Console. writeLine (Enum. parse (typeof (Di), str ). toString ());}

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.