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

VulpesPosted Oct 22, 2014, 9:18 AM
int i = 1;
bool b = (bool)i;
and so is this:
if(i)
{
// do something)
}
So, 1 and 0 are int literals and true and false are bool literals.
It's just a convenient convention that we regard 1 as equivalent to true and 0 as equivalent to false as this enables us to map values between the two types.
So, in the latest program, I'm making use of this convention by assigning true to 'sheLovesMe' if num is 1 or false to it if num is 0.
Note in particular that sheLovesMe is not becoming equal to num. It's becoming equal to the bool expression (num == 1) which is a different matter.
MahaPosted Oct 22, 2014, 9:22 AM
MahaPosted Oct 22, 2014, 9:13 AM
VulpesPosted Oct 22, 2014, 8:36 AM
But (num== 1) is an expression of type bool because num is either equal to 1 or it isn't.
MahaPosted Oct 22, 2014, 6:25 AM
VulpesPosted Oct 22, 2014, 6:13 AM
'num' is an int which is either 0 or 1 chosen at random.
(num == 1) is a bool expression which is true if num is equal to 1 or false otherwise (in this case, if num equals 0).
So we're assigning this expression to the bool variable 'sheLovesMe' and then switching on it and printing out the appropriate message to the console.
MahaPosted Oct 22, 2014, 4:12 AM
In the following code num is bool type as well as int type. Could you explain this variations.
bool sheLovesMe = (num == 1);
Usually we say 0 is false and 1 is true. I wish to know whether this concept is used in the above program.
VulpesPosted Oct 21, 2014, 4:52 PM
MahaPosted Oct 21, 2014, 1:54 PM
VulpesPosted Oct 21, 2014, 12:05 PM
This is reasonable enough given that enums always have an underlying integral type.
Although it's completely undocumented, I think you can also switch on the 'bool' type as well but, given there are only two possible values: true and false, an if/else is always better :)