C# 开关 vs. VB Case 语句 [英] C# Switches vs. VB Case Statements

查看:44
本文介绍了C# 开关 vs. VB Case 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从 VB 切换到 C#.我注意到的一件事是,在 C# 中,我在使用比较作为案例的一部分时遇到了问题.我不知道如何用语言来解释它,所以这是我想要做的一个例子.

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.

在 VB 中,我的代码看起来像这样并且运行良好.

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

另一方面,在 C# 中,Visual Studio 告诉我 ">=" 是一个无效的表达式.

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

我在这里做错了什么,还是在 C# 中根本不可能做到这一点?

Am I doing something wrong here, or is it simply not possible to do this in C#?

在此先非常感谢您!

推荐答案

此答案的顶部部分适用于 7 之前的 C# 版本.请参阅以下行以获取版本 7 的更新

这不可能.C# 开关只能打开完全相等:

It's not possible. C# switches can only switch on exact equality:

每个案例标签指定一个常量值.控制被转移到 switch 部分,它的 case 标签包含一个与 switch 表达式的值匹配的常量值,

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,

你可以用一堆 if/else 语句替换它,或者如果你愿意,你可以做一些看起来很紧凑的东西,但有些人可能会皱眉 -条件运算符的嵌套:

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";

<小时>

在 C# 7 中,switch 已经显着增强,现在可以在 case 中应用更多条件,尽管它仍然不如干净"作为VB版本.例如.你可以这样做:


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

假设 examScore 是一个 int,这在某种程度上滥用了新的类型匹配模式"工具,以便能够somethingcase 子句中说,然后使用 when 子句将任意条件应用于新引入的变量.

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.

这篇关于C# 开关 vs. VB Case 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆