Check if an integer value is defined for Enum - CSharp Custom Type

CSharp examples for Custom Type:Enum

Description

Check if an integer value is defined for Enum

static bool IsFlagDefined (Enum e)
{
  decimal d;
  return !decimal.TryParse(e.ToString(), out d);
}

[Flags]
public enum Directions { Left=1, Right=2, Top=4, Bottom=8 }
static void Main()
{
  for (int i = 0; i <= 16; i++)
  {
    Directions side = (Directions)i;
    Console.WriteLine (IsFlagDefined (side) + " " + side);
  }                                                                                           
}                                                                                             

Related Tutorials