Skip to content

Instantly share code, notes, and snippets.

@GregTrevellick
Created January 13, 2018 08:25
Show Gist options
  • Save GregTrevellick/5c48292bb704bc6378efc3fb44c3876d to your computer and use it in GitHub Desktop.
Save GregTrevellick/5c48292bb704bc6378efc3fb44c3876d to your computer and use it in GitHub Desktop.
using System;
class Program
{
enum PetType
{
None,
Cat = 1,
Dog = 2
}
static void Main()
{
// A.
// Possible user input:
string value = "Dog";
// B.
// Try to convert the string to an enum:
PetType pet = (PetType)Enum.Parse(typeof(PetType), value);
// C.
// See if the conversion succeeded:
if (pet == PetType.Dog)
{
Console.WriteLine("Equals dog.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment