C# - Switch 语句

当您想针对三个或更多条件测试变量时,可以使用 switch 语句代替 if else 语句。 在这里,您将了解 switch 语句以及如何在 C# 程序中有效地使用它。

以下是switch语句的一般语法

switch(匹配表达式/变量的值)
{
    case 常量:
       一些语句
        break;
    case 常量2:
       一些语句
        break;
    default: 
         一些语句
        break;
}

switch 是一个选择语句,它根据与匹配表达式匹配的模式,从候选列表中选择单个开关部分进行执行。 每个 case 包含一个或多个要执行的语句。 如果常量值和 匹配表达式/变量的值相等,则将执行该case下的语句。 switch还可以包含一个可选的default case。 如果上面的的条件都没有满足,则会执行 default里的语句。 可以使用 break, return , goto 跳出 switch

以下示例演示了一个简单的 switch 语句。

int x = 10;

switch (x)
{ 
    case 5:
        Console.WriteLine("Value of x is 5");
        break;
    case 10:
        Console.WriteLine("Value of x is 10");
        break;
    case 15:
        Console.WriteLine("Value of x is 15");
        break;
    default:
        Console.WriteLine("Unknown value");
        break;
}

输出

Value of x is 10

上面,该语句包含一个变量x,其值将与每个 case 值的值相匹配。 上面的语句包含三个具有常量值 5、10 和 15 的 case。它还包含默认标签,如果没有 case 值与 switch 变量/表达式匹配,则将执行默认标签。 每个case后都包含了一个要执行的语句。 X的值是5与第二种情况匹配,因此输出将为 Value of x is 10

该语句还可以包含一个表达式,其结果将在运行时针对每种情况进行测试。

int x = 125;

switch (x % 2)
{ 
    case 0:
        Console.WriteLine($"{x} is an even value");
        break;
    case 1:
        Console.WriteLine($"{x} is an odd Value");
        break;
}

输出

125 is an odd value

Switch case的规则

switch case 必须是唯一的常量值。 它可以是 bool、char、string、integer、enum 或相应的可空类型。

在 C# 7.0之后不在要求唯一值。 有一些case需要执行相同的语句,这个时候可以把它们合并在一起。 例始下面的 4 和 5

int x = 5;

switch (x)
{ 
    case 1:
        Console.WriteLine("x = 1");
        break;
    case 2:
        Console.WriteLine("x = 2");
        break;
    case 4:
    case 5:
        Console.WriteLine("x = 4 or x = 5");
        break;
    default:
        Console.WriteLine("x > 5");
        break;
}

输出 x = 4 or x = 5

使用return来退出的示例

static void Main(string[] args)
{
    int x = 125;
    Console.Write( isOdd(x)? "Even value" : "Odd value");
}

static bool isOdd(int i, int j)
{
    switch (x % 2)
    { 
        case 0:
            return true;
        case 1:
            return false;
        default:
            return false;
    }
    
    return false;
}

输出

Odd value

每个 case 必须通过使用 break 语句或其他方式显式退出 case.如果漏写了break.或者其它退出case的语句。编译器会报错 CS0163 控制不能从一个 case 标签(“case 5:”)贯穿到另一个 case 标签 如下示例

int x = 5;

switch (x)
{ 
    case 1:
        Console.WriteLine("x = 1");
        break;
    case 2:
        Console.WriteLine("x = 2");
        break;
    case 4:
    case 5:
        Console.WriteLine("x = 4 or x = 5");
        //这边忘记退出case了
    default:
        Console.WriteLine("x > 5");
        break;
}

嵌套的 Switch 语句 一个语句可以在另一个语句中使用。

int j = 5;

switch (j)
{
    case 5:
        Console.WriteLine(5);
        switch (j - 1)
        {
            case 4:
            Console.WriteLine(4);
            switch (j - 2)
            {
                case 3:
                Console.WriteLine(3);
                break;
            }
            break;
        }
        break;
    case 10:
        Console.WriteLine(10);
        break;
    case 15:
        Console.WriteLine(15);
        break;
    default:
        Console.WriteLine(100);
        break;
}

输出

5
4
3

要记住的点:

  • switch 是用来替代 if else的,这样可以让代码更易读
  • 该语句使用一组的cases来测试 表达式 和变量
  • case 案例必须包含 break、return、goto 关键字以退出case。
  • 可以包含一个可选的default case,该标签将在没有 case 执行时执行。
  • 从 C# 7.0 开始,switch case 可以包含非唯一值。 在这种情况下,将执行第一个匹配的案例。
下一篇:C# for 循环
最近更新的
...