C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
IsImportant: This method contains a switch that tests the parameter of type Priority. It returns true if the Priority value is Important or Critical.
True, FalseSo: You can use the switch here as a kind of filtering mechanism for enum value ranges.
Bool MethodC# program that switches on enum
using System;
enum Priority
{
Zero,
Low,
Medium,
Important,
Critical
};
class Program
{
static void Main()
{
// New local variable of the Priority enum type.
Priority priority = Priority.Zero;
// Set priority to critical on Monday.
if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
{
priority = Priority.Critical;
}
// Write this if the priority is important.
if (IsImportant(priority))
{
Console.WriteLine("The problem is important.");
}
// See if Low priority is important.
priority = Priority.Low;
Console.WriteLine(IsImportant(priority));
// See if Important priority is.
priority = Priority.Important;
Console.WriteLine(IsImportant(priority));
}
static bool IsImportant(Priority priority)
{
// Switch on the Priority enum.
switch (priority)
{
case Priority.Low:
case Priority.Medium:
case Priority.Zero:
default:
return false;
case Priority.Important:
case Priority.Critical:
return true;
}
}
}
Output
The problem is important.
False
True
Switch instruction: IL
L_0004: switch (L_001f, L_001f, L_001f, L_0023, L_0023)