I recently switched from VB to C#. One thing that I noticed was that in C#, I have problems using comparisons as part of the case. I am not sure how to explain it in words, so here is an example of what I am trying to do.
In VB, my code looks like this and works perfectly fine.
Select Case ExamScore
Case Is >= 90
Grade = "A"
Case Is >= 80
Grade = "B"
Case Is >= 70
Grade = "C"
Case Is >= 60
Grade = "D"
Case Else
Grade = "F"
End Select
In C# on the other hand, Visual Studio tells me that ">=" is an invalid expression.
switch (examScore)
{
case >= 90: grade = "A"; break;
case >= 80: grade = "B"; break;
case >= 70: grade = "C"; break;
case >= 60; grade = "D"; break;
default: grade = "F"; break;
}
Am I doing something wrong here, or is it simply not possible to do this in C#?
Thank you very much in advance!
Top part of this answer is true for C# versions before 7. See below the line for an update for version 7
It's not possible. C# switches can only switch on exact equality:
Each case label specifies a constant value. Control is transferred to the switch section whose case label contains a constant value that matches the value of the switch expression,
You could replace it with a stack of if
/else
statements, or if you prefer, you can make something that looks quite compact, but some may frown on - a nest of conditional operators:
grade = examScore >= 90 ? "A" :
examScore >= 80 ? "B" :
examScore >= 70 ? "C" :
examScore >= 60 ? "D" :
"F";
With C# 7, switch
has been significantly enhanced, and it's now possible to apply more conditions within case
s, although it's still not as "clean" as the VB version. E.g. you could do something like:
switch (examScore)
{
case int es when es >= 90: grade = "A"; break;
case int es when es >= 80: grade = "B"; break;
case int es when es >= 70: grade = "C"; break;
case int es when es >= 60; grade = "D"; break;
default: grade = "F"; break;
}
Assuming that examScore
is an int
, this somewhat abuses the new "pattern matching on types" facility to be able to have something to say in the case
clause, and then using the when
clauses to apply arbitrary conditions to the newly introduced variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With