Maha

Maha

  • NA
  • 600
  • 70.8k

Switch statement

Oct 21 2014 11:58 AM
http://www.dotnetperls.com/switch

In the above webpage under the CAUTION heading it says that expression in the switch must be int/char/string.

In the following program different type is used in the switch expression. Could you explain the difference please? Problem is highlighted.  

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));

Console.ReadKey();
}

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;
}
}
}


Answers (10)