Esempio n. 1
0
        static void Main(string[] args)
        {
            //<snippet4>
            Day meetingDay = Day.Monday;

            //...
            meetingDay = Day.Friday;
            //</snippet4>
            Console.WriteLine($"Meeting day is {meetingDay}");

            Console.WriteLine($"Meeting day is {(int) meetingDay}");

            meetingDay = (Day)42;
            Console.WriteLine($"Meeting day is {meetingDay}");

            Days meetingDays = Days.Tuesday | Days.Thursday;

            Console.WriteLine(meetingDays);

            //<snippet5>
            string s = Enum.GetName(typeof(Day), 4);

            Console.WriteLine(s);
            // Output: Thursday

            Console.WriteLine("The values of the Day Enum are:");
            foreach (int i in Enum.GetValues(typeof(Day)))
            {
                Console.WriteLine(i);
            }
            // Output:
            // The values of the Day Enum are:
            // 0
            // 1
            // 2
            // 3
            // 4
            // 5
            // 6

            Console.WriteLine("The names of the Day Enum are:");
            foreach (string str in Enum.GetNames(typeof(Day)))
            {
                Console.WriteLine(str);
            }
            // Output:
            // The names of the Day Enum are:
            // Sunday
            // Monday
            // Tuesday
            // Wednesday
            // Thursday
            // Friday
            // Saturday
            //</snippet5>

            //<snippet6>
            // Initialize with two flags using bitwise OR.
            meetingDays = Days.Tuesday | Days.Thursday;

            // Set an additional flag using bitwise OR.
            meetingDays = meetingDays | Days.Friday;

            Console.WriteLine($"Meeting days are {meetingDays}");
            // Output: Meeting days are Tuesday, Thursday, Friday

            // Toggle a flag using bitwise XOR.
            meetingDays = meetingDays ^ Days.Tuesday;
            Console.WriteLine($"Meeting days are {meetingDays}");
            // Output: Meeting days are Thursday, Friday
            //</snippet6>

            //<snippet7>
            // Test value of flags using bitwise AND.
            bool test = (meetingDays & Days.Thursday) == Days.Thursday;

            Console.WriteLine($"Thursday {(test == true ? "is" : "is not")} a meeting day.");
            // Output: Thursday is a meeting day.
            //</snippet7>

            Console.ReadKey();
        }
Esempio n. 2
0
        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();
        }