Вопрос

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 cases, 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.

Другие советы

Unlike in VB, the C# switch statement is something like "equals" check. So you might need a if else ladder in order to accomplish this.

You may 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';
}

It's not possible in C#.

Use a bunch of ifs instead.

You can have it all in 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";
}

If you really want a switch statement you could use integer division

        int i = 69;
        switch (Math.Min(9, i / 10))
        {
            case 9: Grade = "A"; break;
            case 6: Grade = "B"; break;
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top