c# - 如何在一个案例中处理多个值?

标签 c# .net switch-statement case-statement

<分区>

如何在一个case 中处理多个值?因此,如果我想对值 "first option""second option" 执行相同的操作?

这是正确的方法吗?

switch(text)
{
    case "first option":
    {
    }
    case "second option":
    {
        string a="first or Second";
        break;
    }
}

最佳答案

它在文档中称为“多个标签”,可以在C# documentation 中找到。在 MSDN 上。

A switch statement can include any number of switch sections, and each section can have one or more case labels (as shown in the string case labels example below). However, no two case labels may contain the same constant value.

您修改后的代码:

string a = null;

switch(text)
{
    case "first option":
    case "second option":
    {
        a = "first or Second";
        break;
    }
}

请注意,我拉出了 string a,否则你的 a 将只能在 switch 中使用。

关于c# - 如何在一个案例中处理多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943658/

相关文章:

c# - SharpDx:设置字符宽度

c# - 带或不带 .ToString() 的字符串连接?

c# - 有没有办法在其内部重建一个对象?

javascript - 使用 ExtJS 切换 View

c# - 如何将带有子查询的 SQL 查询转换为 .NET Core 查询

C# - 将指向 sockaddr 结构的 IntPtr 转换为 IPAddress

c++ - 如何在 switch 语句中使用随机生成的数字?

android - 如何在Android中更改开关的文本颜色

c# - Sqlite 一对多关系

.NET:缺少必需的配置设置时抛出哪个异常?