user1928185
user1928185

Reputation: 112

Convert Enum to string in c#

I have an enum class. I am using C#. I need to convert it from enum to string. For example, Language.En will return as string.

public enum Language
{
    EN,
    SV,
    DE
}

Upvotes: 1

Views: 1457

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 453

//try this :)

using System;

enum Priority
{
    never,
    killed,
    beauty
}

class Program
{
    static void Main()
    {
    // Write string representation for killed.
    Priority enum1 = Priority.killed;
    string value1 = enum1.ToString();

    // Loop through enumerations and write string representations
    for (Priority enum2 = Priority.never; enum2 <= Priority.beauty; enum2++)
    {
        string value2 = enum2.ToString();
        Console.WriteLine(value2);
    }
    }
}

Output
never
killed
beauty

Upvotes: 1

David L
David L

Reputation: 33823

Just call ToString() on the Enum.

Language.EN.ToString();

Upvotes: 9

Related Questions