static void Main() { EnumTest tstEnum = EnumTest.Unknown; object objTestEnum; objTestEnum = Enum.Parse(tstEnum.GetType(), "EnumThree"); if (objTestEnum is EnumTest) { EnumTest newTestEnum = (EnumTest)objTestEnum; Console.WriteLine("newTestEnum = {0}", newTestEnum.ToString()); } }
static void TraverseDemo() { // It is the reason of setting int 1, 2, 4, 8... for an enum: // We can combine multiple states with the "|" operator EnumTest three = EnumTest.One | EnumTest.One | EnumTest.Two; // If there is not [Flags] tab, here will print "3". Console.WriteLine(three.ToString()); foreach (EnumTest e in Enum.GetValues(three.GetType())) { if (three.HasFlag(e)) { Console.WriteLine(e); } } }