C
C#2d ago
Faker

Switch statements

Hello, consider the following code:
C#
int employeeLevel = 200;
string employeeName = "John Smith";

string title = "";

switch (employeeLevel)
{
case 100:
title = "Junior Associate";
break;
case 200:
title = "Senior Associate";
break;
case 300:
title = "Manager";
break;
case 400:
title = "Senior Manager";
break;
default:
title = "Associate";
break;
}

Console.WriteLine($"{employeeName}, {title}");
C#
int employeeLevel = 200;
string employeeName = "John Smith";

string title = "";

switch (employeeLevel)
{
case 100:
title = "Junior Associate";
break;
case 200:
title = "Senior Associate";
break;
case 300:
title = "Manager";
break;
case 400:
title = "Senior Manager";
break;
default:
title = "Associate";
break;
}

Console.WriteLine($"{employeeName}, {title}");
Can the switch expression be a expression that evaluates to true or false? like age < 5? or the case pattern can they be an expression that evaluates to true or false like age < 5?
21 Replies
Thinker
Thinker2d ago
Firstly, a switch expression is something different from what you're using here (although you could actually use a switch expression here!), what you're using is a switch statement
Faker
FakerOP2d ago
ahh these 2 are 2 different things my bad sorry, thought switch expression and switch statements were the same but on microsoft learn they mentioned switch statement and not expression, my bad
Thinker
Thinker2d ago
A switch expression would be something like this
string title = employeeLevel switch
{
100 => "Junior Associate",
200 => "Senior Associate",
// ...
_ => "Associate"
};
string title = employeeLevel switch
{
100 => "Junior Associate",
200 => "Senior Associate",
// ...
_ => "Associate"
};
Faker
FakerOP2d ago
oh ok is that still use nowadays or swtich statement are prefered?
Angius
Angius2d ago
Different uses
Thinker
Thinker2d ago
They're two different things, both can be used just fine Your specific case right here is kinda perfect for switch expressions though
Faker
FakerOP2d ago
ahh I see switch statement can evaluate things like "age >5" etc/
Thinker
Thinker2d ago
Switch statements are useful when you want to execute different code (eg. writing to the console) in different branches of the switch, while switch expressions are used to return different singular values depending on the branch.
Faker
FakerOP2d ago
ahhh
Thinker
Thinker2d ago
yes... although I'm not entirely sure what you mean right here
Faker
FakerOP2d ago
it is returned to what, to title ?
Thinker
Thinker2d ago
You can do this, yes
switch (age)
{
case > 5:
// ...
}
switch (age)
{
case > 5:
// ...
}
yep
Faker
FakerOP2d ago
yeah I see, let's say we want a range, like age between 5 and 10 we won't be able to use switch statements in this case?
Thinker
Thinker2d ago
yes you would Specifically for numbers, you can write things like case >= 5 and <= 10
Faker
FakerOP2d ago
oh ok
Thinker
Thinker2d ago
which will execute the branch if the value is between 5 and 10
Faker
FakerOP2d ago
the thing is I think in other programming language like Java, I think there we can't do that, that's why I was having a doubt
Thinker
Thinker2d ago
Yeah, C#'s switch statements are a bit more versatile that in a lot of other languages
Faker
FakerOP2d ago
yep I see, noted, thanks !!
Thinker
Thinker2d ago
Since they can match a lot more stuff If you wanna read more into this, it's called "pattern matching" It's incredibly powerful and can be used to great extent to achieve some very cool things
Faker
FakerOP2d ago
yep, will read a bit about that, seems interesting and quick syntax noted, will try to read about them, then, ty !

Did you find this page helpful?