private static void FlagsEnum() { Console.WriteLine("Using Enum w/ Flags Decorate"); Days2 d = Days2.Monday | Days2.Saturday; Console.WriteLine("Days that can read {0}", d.ToString()); }
static void Main(string[] args) { Days1 today = Days1.Sunday; Console.WriteLine(today); Console.WriteLine((int)today); Days2 days = Days2.Monday | Days2.Tuesday | Days2.Wednesday; Console.WriteLine(days.HasFlag(Days2.Sunday)); Console.WriteLine(days.HasFlag(Days2.Monday)); Console.WriteLine(days); }
static void Main(string[] args) { //<snippet4> Days meetingDay = Days.Monday; //... meetingDay = Days.Friday; //</snippet4> Console.WriteLine("Meeting day is {0}", meetingDay); Console.WriteLine("Meeting day is {0}", (int)meetingDay); meetingDay = (Days)42; Console.WriteLine("Meeting day is {0}", meetingDay); Days2 meetingDays = Days2.Tuesday | Days2.Thursday; Console.WriteLine(meetingDays); //<snippet5> string s = Enum.GetName(typeof(Days), 4); Console.WriteLine(s); Console.WriteLine("The values of the Days Enum are:"); foreach (int i in Enum.GetValues(typeof(Days))) { Console.WriteLine(i); } Console.WriteLine("The names of the Days Enum are:"); foreach (string str in Enum.GetNames(typeof(Days))) { Console.WriteLine(str); } //</snippet5> //<snippet6> // Initialize with two flags using bitwise OR. meetingDays = Days2.Tuesday | Days2.Thursday; // Set an additional flag using bitwise OR. meetingDays = meetingDays | Days2.Friday; Console.WriteLine("Meeting days are {0}", meetingDays); // Output: Meeting days are Tuesday, Thursday, Friday // Remove a flag using bitwise XOR. meetingDays = meetingDays ^ Days2.Tuesday; Console.WriteLine("Meeting days are {0}", meetingDays); // Output: Meeting days are Thursday, Friday //</snippet6> //<snippet7> // Test value of flags using bitwise AND. bool test = (meetingDays & Days2.Thursday) == Days2.Thursday; Console.WriteLine("Thursday {0} a meeting day.", test == true ? "is" : "is not"); // Output: Thursday is a meeting day. //</snippet7> Console.ReadKey(); }
public static void TryFlags() { Days2 readingDays = Days2.Monday | Days2.Saturday; Console.WriteLine(readingDays); }