Enumerations with tryParse in C#

      No Comments on Enumerations with tryParse in C#

Here is an example of using enum with a tryParse. The program asks the user to enter a new state. The tryParse then attempts to convert the input into a enum.

// define current state
MyState currentState = MyState.off;
// get value from user
Console.WriteLine("Please enter the new state");
string input = Console.ReadLine();

if (Enum.TryParse(input, out currentState) == false)
    Console.WriteLine("Unable to compute input");
Console.WriteLine("Current state " + currentState);

Console.WriteLine("=== Finished ===");

enum MyState

Leave a Reply

Your email address will not be published. Required fields are marked *