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 ());}