C# Enums – Getting the Integer Value from Enum

c++enums

I am working on a basic Battleship game to help my C# skills. Right now I am having a little trouble with enum. I have:

enum game : int
{
    a=1,
    b=2,
    c=3,
}

I would like the player to pass the input "C" and some code return the integer 3. How would I set it up for it to take a string var (string pick;) and convert it to the correct int using this enum? The book I am reading on this is bit confusing

Best Answer

Just parse the string and cast to int.

var number = (int)((game) Enum.Parse(typeof(game), pick));
Related Question