Skip to main content

How to create an Enum value from the string

This is a simple code for converting string representation to Enum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Blog_CS
{
    public enum State { New, Ready, Stop, Reset };
    class Program
    {
        private static void Main(string[] args)
        {
            const string state = "New";
            State currentState = (State)Enum.Parse(typeof(State), state);

            if (currentState == State.New)
            {
                Console.WriteLine("Run state");

            }
        }
    }
}

Comments

Popular posts from this blog