C # or VB Case Switches

I recently switched from VB to C #. One thing I noticed is that in C # I have problems using comparisons as part of the case. I'm not sure how to explain it in words, so here is an example of what I'm trying to do.

In VB, my code looks and works just 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 just impossible to do in C #?

Thank you in advance!

+7
c # switch-statement
source share
5 answers

The top of this answer is correct for C # versions up to 7. See the upgrade line for version 7 below.

It's impossible. C # switches can only include exact equality:

Each case label indicates a constant value. The control is passed to the switch section, the label of which contains a constant value that corresponds to the value of the switch expression,

You can replace it with a stack of if / else , or if you want, you can do something that looks pretty compact, but some may frown - a nest of conditional statements:

 grade = examScore >= 90 ? "A" : examScore >= 80 ? "B" : examScore >= 70 ? "C" : examScore >= 60 ? "D" : "F"; 

With C # 7, the switch has improved significantly , and now you can apply more conditions within case s, although it is still not "clean" like the version of VB. For example. you can 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 examScore is an int , this somewhat abuses the new type-type matching object to be able to say something in the case clause, and then use the when clauses to apply arbitrary conditions to the newly entered variable.

+10
source share

Unlike VB, the C # switch statement is a bit of an equal check. Thus, to achieve this, you may need another staircase.

You can try something like:

 private char Grade(int marks) { Dictionary<int, char> grades = new Dictionary<int, char> { { 60, 'A' }, { 50, 'B' } }; foreach (var item in grades) { if (marks > item.Key) return item.Value; } return 'C'; } 
+3
source share

This is not possible in C #.

Use the if slice instead.

+2
source share

You can use all this in a nice function:

 public string Grade(int examScore) { if(examScore>=90) { return "A"; } if(examScore>=80) { return "B"; } if(examScore>=70) { return "C"; } if(examScore>=60) { return "D"; } return "F"; } 
+1
source share

If you really need a switch statement, you can use integer division

  int i = 69; switch (Math.Min(9, i / 10)) { case 9: Grade = "A"; break; case 6: Grade = "B"; break; } 
+1
source share

All Articles