Convert enum to string in C#

In this article we will see how to convert an enum to string value using C#.
  • 4487

Here we will convert from enum to string variable. For this purpose we will use ToString() method.
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
    class Program
    {
        enum Colors { Green = 1, Yellow = 2 };
        static void Main(string[] args)
        {
            Enum myColors = Colors.Yellow;
            Console.WriteLine("The value of this instance is '{0}'",
            myColors.ToString());
            Console.ReadLine();
        }
    }
}

 

  
The output of the above code would be The value of this instance is 'Yellow'.

Ask Your Question 

 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.