c#语言swith的用法,C# switch 语句 | Microsoft Docs

switch(C# 参考)

04/09/2019

本文内容

本文介绍 switch 语句。 有关 switch 表达式(在 C# 8.0 中引入)的信息,请参阅 表达式和运算符部分中有关 switch 表达式的文章。

switch 是一个选择语句,它根据与匹配表达式匹配的模式,从候选列表中选择单个开关部分进行执行。

using System;

public class Example

{

public static void Main()

{

int caseSwitch = 1;

switch (caseSwitch)

{

case 1:

Console.WriteLine("Case 1");

break;

case 2:

Console.WriteLine("Case 2");

break;

default:

Console.WriteLine("Default case");

break;

}

}

}

// The example displays the following output:

// Case 1

如果针对 3 个或更多条件测试单个表达式,switch 语句通常用作 if-else 构造的替代项。 例如,以下 switch 语句确定类型为 Color 的变量是否具有三个值之一:

using System;

public enum Color { Red, Green, Blue }

public class Example

{

public static void Main()

{

Color c = (Color) (new Random()).Next(0, 3);

switch (c)

{

case Color.Red:

Console.WriteLine("The color is red");

break;

case Color.Green:

Console.WriteLine("The color is green");

break;

case Color.Blue:

Console.WriteLine("The color is blue");

break;

default:

Console.WriteLine("The color is unknown.");

break;

}

}

}

它相当于使用 if-else 构造的以下示例。

using System;

public enum Color { Red, Green, Blue }

public class Example

{

public static void Main()

{

Color c = (Color) (new Random()).Next(0, 3);

if (c == Color.Red)

Console.WriteLine("The color is red");

else if (c == Color.Green)

Console.WriteLine("The color is green");

else if (c == Color.Blue)

Console.WriteLine("The color is blue");

else

Console.WriteLine("The color is unknown.");

}

}

// The example displays the following output:

// The color is red

匹配表达式

匹配表达式提供与 case 标签中的模式相匹配的值。 语法为:

switch (expr)

在 C# 6 及更低版本中,匹配表达式必须是返回以下类型值的表达式:

整数值,例如 int 或 long。

枚举值。

从 C# 7.0 开始,匹配表达式可以是任何非 null 表达式。

开关部分

switch 语句包含一个或多个开关部分。 每个 switch 部分包含一个或多个 case 标签(case 或 default 标签),后接一个或多个语句。 switch 语句最多可包含一个置于任何 switch 部分中的 default 标签。 以下示例显示了一个简单的 switch 语句,该语句包含三个 switch 部分,每个部分包含两个语句。 第二个 switch 部分包含 case 2: 和 case 3: 标签。

switch 语句中可以包含任意数量的开关部分,每个开关部分可以具有一个或多个 case 标签,如以下示例所示。 但是,任何两个 case 标签不可包含相同的表达式。

using System;

public class Example

{

public static void Main()

{

Random rnd = new Random();

int caseSwitch = rnd.Next(1,4);

switch (caseSwitch)

{

case 1:

Console.WriteLine("Case 1");

break;

case 2:

case 3:

Console.WriteLine($"Case {caseSwitch}");

break;

default:

Console.WriteLine($"An unexpected value ({caseSwitch})");

break;

}

}

}

// The example displays output like the following:

// Case 1

switch 语句执行中只有一个开关部分。 C# 禁止从一个 switch 部分继续执行到下一个 switch 部分。 因此,下面的代码生成编译器错误 CS0163:“控件不能从一个 case 标签 () 贯穿到另一个 case 标签。”

switch (caseSwitch)

{

// The following switch section causes an error.

case 1:

Console.WriteLine("Case 1...");

// Add a break or other jump statement here.

case 2:

Console.WriteLine("... and/or Case 2");

break;

}

通常通过使用 break、goto 或 return 语句显式退出开关部分来满足此要求。 不过,以下代码也有效,因为它确保程序控制权无法贯穿到 default switch 部分。

switch (caseSwitch)

{

case 1:

Console.WriteLine("Case 1...");

break;

case 2:

case 3:

Console.WriteLine("... and/or Case 2");

break;

case 4:

while (true)

Console.WriteLine("Endless looping. . . .");

default:

Console.WriteLine("Default value...");

break;

}

在 case 标签与匹配表达式匹配的开关部分中执行语句列表时,先执行第一个语句,再执行整个语句列表,通常执行到跳转语句(如 break、goto case、goto label、return 或 throw)为止。 此时,控件在 switch 语句之外进行传输或传输到另一个 case 标签。 如果使用的是 goto 语句,必须将控制权移交给常量标签。 此限制是必要的,因为尝试将控制权移交给非常数标签可能会产生不良的副作用,如将控制权移交给代码中的意外位置,或创建无限循环。

Case 标签

每个 case 标签指定一个模式与匹配表达式(前面示例中的 caseSwitch 变量)进行比较。 如果它们匹配,则将控件传输到包含首次匹配 case 标签的开关部分。 如果 case 标签模式与匹配表达式不匹配,控制权会转让给带 default case 标签的部分(若有)。 如果没有 default case,将不会执行任何 switch 部分中的语句,并且会将控制权转让到 switch 语句之外。

有关 switch 语句和模式匹配的信息,请参阅使用 switch 语句的 模式匹配部分。

因为 C# 6 仅支持常量模式且禁止重复常量值,所以 case 标签定义了互斥值,而且只能有一个模式与匹配表达式匹配。 因此,case 语句显示的顺序并不重要。

然而,在 C# 7.0 中,因为支持其他模式,所以 case 标签不需要定义互斥值,并且多个模式可以与匹配表达式相匹配。 因为仅执行包含匹配模式的首次开关部分中的语句,所以 case 语句显示的顺序很重要。 如果 C# 检测到开关部分的 case 语句或语句等效于或是先前语句的子集,它将生成编译错误 CS8120:“开关 case 已由先前 case 处理。”

以下示例说明了使用各种非互斥模式的 switch 语句。 如果你移动 case 0: switch 部分,使之不再是 switch 语句中的第一部分,C# 会生成编译器错误,因为值为零的整数是所有整数的子集(由 case int val 语句定义的模式)。

using System;

using System.Collections.Generic;

using System.Linq;

public class Example

{

public static void Main()

{

var values = new List();

for (int ctr = 0; ctr <= 7; ctr++) {

if (ctr == 2)

values.Add(DiceLibrary.Roll2());

else if (ctr == 4)

values.Add(DiceLibrary.Pass());

else

values.Add(DiceLibrary.Roll());

}

Console.WriteLine($"The sum of { values.Count } die is { DiceLibrary.DiceSum(values) }");

}

}

public static class DiceLibrary

{

// Random number generator to simulate dice rolls.

static Random rnd = new Random();

// Roll a single die.

public static int Roll()

{

return rnd.Next(1, 7);

}

// Roll two dice.

public static List Roll2()

{

var rolls = new List();

rolls.Add(Roll());

rolls.Add(Roll());

return rolls;

}

// Calculate the sum of n dice rolls.

public static int DiceSum(IEnumerable values)

{

var sum = 0;

foreach (var item in values)

{

switch (item)

{

// A single zero value.

case 0:

break;

// A single value.

case int val:

sum += val;

break;

// A non-empty collection.

case IEnumerable subList when subList.Any():

sum += DiceSum(subList);

break;

// An empty collection.

case IEnumerable subList:

break;

// A null reference.

case null:

break;

// A value that is neither an integer nor a collection.

default:

throw new InvalidOperationException("unknown item type");

}

}

return sum;

}

public static object Pass()

{

if (rnd.Next(0, 2) == 0)

return null;

else

return new List();

}

}

你可以通过以下两种方法之一更正此问题并消除编译器警告:

更改开关部分的顺序。

在 case 标签中使用 when clause 子句。

default case

如果匹配表达式与其他任何 case 标签都不匹配,default case 指定要执行的 switch 部分。 如果没有 default case,且匹配表达式与其他任何 case 标签都不匹配,程序流就会贯穿 switch 语句。

default case 可以在 switch 语句中以任何顺序显示。 无论它在源代码中的顺序如何,始终都将在计算所有 case 标签后,最后计算它。

使用 switch 语句的模式匹配

每个 case 语句定义一个模式,如果它与匹配表达式相匹配,则会导致执行其包含的开关部分。 所有版本的 C# 都支持常量模式。 其余模式从 C# 7.0 开始支持。

常量模式

常量模式测试匹配表达式是否等于指定常量。 语法为:

case constant:

其中 constant 是要测试的值。 constant 可以是以下任何常数表达式:

bool 文本:true 或 false。

任何整型常数,例如 int、long 或 byte。

已声明 const 变量的名称。

一个枚举常量。

字符型文本。

字符串文本。

常数表达式的计算方式如下:

如果 expr 和 constant 均为整型类型,则 C# 相等运算符确定表示式是否返回 true(即,是否为 expr == constant)。

以下示例使用常量模式来确定特定日期是否为周末、工作周的第一天、工作周的最后一天或工作周的中间日期。 它根据 DayOfWeek 枚举的成员计算当前日期的 DateTime.DayOfWeek 属性。

using System;

class Program

{

static void Main()

{

switch (DateTime.Now.DayOfWeek)

{

case DayOfWeek.Sunday:

case DayOfWeek.Saturday:

Console.WriteLine("The weekend");

break;

case DayOfWeek.Monday:

Console.WriteLine("The first day of the work week.");

break;

case DayOfWeek.Friday:

Console.WriteLine("The last day of the work week.");

break;

default:

Console.WriteLine("The middle of the work week.");

break;

}

}

}

// The example displays output like the following:

// The middle of the work week.

以下示例使用常量模式在模拟自动咖啡机的控制台应用程序中处理用户输入。

using System;

class Example

{

static void Main()

{

Console.WriteLine("Coffee sizes: 1=small 2=medium 3=large");

Console.Write("Please enter your selection: ");

string str = Console.ReadLine();

int cost = 0;

// Because of the goto statements in cases 2 and 3, the base cost of 25

// cents is added to the additional cost for the medium and large sizes.

switch (str)

{

case "1":

case "small":

cost += 25;

break;

case "2":

case "medium":

cost += 25;

goto case "1";

case "3":

case "large":

cost += 50;

goto case "1";

default:

Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");

break;

}

if (cost != 0)

{

Console.WriteLine("Please insert {0} cents.", cost);

}

Console.WriteLine("Thank you for your business.");

}

}

// The example displays output like the following:

// Coffee sizes: 1=small 2=medium 3=large

// Please enter your selection: 2

// Please insert 50 cents.

// Thank you for your business.

类型模式

类型模式可启用简洁类型计算和转换。 使用 switch 语句执行模式匹配时,会测试表达式是否可转换为指定类型,如果可以,则将其转换为该类型的一个变量。 语法为:

case type varname

其中 type 是 expr 结果要转换到的类型的名称,varname 是 expr 结果要转换到的对象(如果匹配成功)。 自 C# 7.1 起,expr 的编译时类型可能为泛型类型参数。

如果以下任一条件成立,则 case 表达式为 true:

expr 是与 type 具有相同类型的一个实例。

expr 是派生自 type 的类型的一个实例。 换言之,expr 结果可以向上转换为 type 的一个实例。

expr 具有属于 type 的一个基类的编译时类型,expr 还具有属于 type 或派生自 type 的运行时类型。 变量的编译时类型是其类型声明中定义的变量类型。 变量的运行时类型是分配给该变量的实例类型。

expr 是实现 type 接口的类型的一个实例。

如果 case 表达式为 true,将会明确分配 varname,并且仅在开关部分中具有本地作用域。

请注意,null 与任何类型都不匹配。 若要匹配 null,请使用以下 case 标签:

case null:

以下示例使用类型模式来提供有关各种集合类型的信息。

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

class Example

{

static void Main(string[] args)

{

int[] values = { 2, 4, 6, 8, 10 };

ShowCollectionInformation(values);

var names = new List();

names.AddRange(new string[] { "Adam", "Abigail", "Bertrand", "Bridgette" });

ShowCollectionInformation(names);

List numbers = null;

ShowCollectionInformation(numbers);

}

private static void ShowCollectionInformation(object coll)

{

switch (coll)

{

case Array arr:

Console.WriteLine($"An array with {arr.Length} elements.");

break;

case IEnumerable ieInt:

Console.WriteLine($"Average: {ieInt.Average(s => s)}");

break;

case IList list:

Console.WriteLine($"{list.Count} items");

break;

case IEnumerable ie:

string result = "";

foreach (var e in ie)

result += $"{e} ";

Console.WriteLine(result);

break;

case null:

// Do nothing for a null.

break;

default:

Console.WriteLine($"A instance of type {coll.GetType().Name}");

break;

}

}

}

// The example displays the following output:

// An array with 5 elements.

// 4 items

可以创建泛型方法,使用集合的类型作为类型参数(而不是使用 object),如下面的代码所示:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

class Example

{

static void Main(string[] args)

{

int[] values = { 2, 4, 6, 8, 10 };

ShowCollectionInformation(values);

var names = new List();

names.AddRange(new string[] { "Adam", "Abigail", "Bertrand", "Bridgette" });

ShowCollectionInformation(names);

List numbers = null;

ShowCollectionInformation(numbers);

}

private static void ShowCollectionInformation(T coll)

{

switch (coll)

{

case Array arr:

Console.WriteLine($"An array with {arr.Length} elements.");

break;

case IEnumerable ieInt:

Console.WriteLine($"Average: {ieInt.Average(s => s)}");

break;

case IList list:

Console.WriteLine($"{list.Count} items");

break;

case IEnumerable ie:

string result = "";

foreach (var e in ie)

result += $"{e} ";

Console.WriteLine(result);

break;

case object o:

Console.WriteLine($"A instance of type {o.GetType().Name}");

break;

default:

Console.WriteLine("Null passed to this method.");

break;

}

}

}

// The example displays the following output:

// An array with 5 elements.

// 4 items

// Null passed to this method.

泛型版本与第一个示例有两点不同。 首先,无法使用 null case。 无法使用任何常量 case 是因为,编译器无法将任意类型 T 转换为除 object 之外的任何类型。 曾经的 default case 现在测试是否有非 null object。 也就是说,default case 测试只针对 null。

如果没有模式匹配,则可能按以下方式编写此代码。 使用类型模式匹配可消除测试转换结果是否为 null 或执行重复转换的必要,从而生成更紧凑易读的代码。

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

class Example

{

static void Main(string[] args)

{

int[] values = { 2, 4, 6, 8, 10 };

ShowCollectionInformation(values);

var names = new List();

names.AddRange(new string[] { "Adam", "Abigail", "Bertrand", "Bridgette" });

ShowCollectionInformation(names);

List numbers = null;

ShowCollectionInformation(numbers);

}

private static void ShowCollectionInformation(object coll)

{

if (coll is Array)

{

Array arr = (Array) coll;

Console.WriteLine($"An array with {arr.Length} elements.");

}

else if (coll is IEnumerable)

{

IEnumerable ieInt = (IEnumerable) coll;

Console.WriteLine($"Average: {ieInt.Average(s => s)}");

}

else if (coll is IList)

{

IList list = (IList) coll;

Console.WriteLine($"{list.Count} items");

}

else if (coll is IEnumerable)

{

IEnumerable ie = (IEnumerable) coll;

string result = "";

foreach (var e in ie)

result += $"{e} ";

Console.WriteLine(result);

}

else if (coll == null)

{

// Do nothing.

}

else

{

Console.WriteLine($"An instance of type {coll.GetType().Name}");

}

}

}

// The example displays the following output:

// An array with 5 elements.

// 4 items

case 语句和 when 子句

从 C# 7.0 开始,因为 case 语句不需要互相排斥,因此可以添加 when 子句来指定必须满足的附加条件使 case 语句计算为 true。 when 子句可以是返回布尔值的任何表达式。

下面的示例定义了 Shape 基类、从 Shape 派生的 Rectangle 类以及从 Rectangle 派生的 Square 类。 它使用 when 子句,以确保 ShowShapeInfo 将已分配相等长度和宽度的 Rectangle 对象视为 Square(即使它尚未实例化为 Square 对象)。 此方法不会尝试显示值为 null 的对象或面积为零的形状的相关信息。

using System;

public abstract class Shape

{

public abstract double Area { get; }

public abstract double Circumference { get; }

}

public class Rectangle : Shape

{

public Rectangle(double length, double width)

{

Length = length;

Width = width;

}

public double Length { get; set; }

public double Width { get; set; }

public override double Area

{

get { return Math.Round(Length * Width,2); }

}

public override double Circumference

{

get { return (Length + Width) * 2; }

}

}

public class Square : Rectangle

{

public Square(double side) : base(side, side)

{

Side = side;

}

public double Side { get; set; }

}

public class Circle : Shape

{

public Circle(double radius)

{

Radius = radius;

}

public double Radius { get; set; }

public override double Circumference

{

get { return 2 * Math.PI * Radius; }

}

public override double Area

{

get { return Math.PI * Math.Pow(Radius, 2); }

}

}

public class Example

{

public static void Main()

{

Shape sh = null;

Shape[] shapes = { new Square(10), new Rectangle(5, 7),

sh, new Square(0), new Rectangle(8, 8),

new Circle(3) };

foreach (var shape in shapes)

ShowShapeInfo(shape);

}

private static void ShowShapeInfo(Shape sh)

{

switch (sh)

{

// Note that this code never evaluates to true.

case Shape shape when shape == null:

Console.WriteLine($"An uninitialized shape (shape == null)");

break;

case null:

Console.WriteLine($"An uninitialized shape");

break;

case Shape shape when sh.Area == 0:

Console.WriteLine($"The shape: {sh.GetType().Name} with no dimensions");

break;

case Square sq when sh.Area > 0:

Console.WriteLine("Information about square:");

Console.WriteLine($" Length of a side: {sq.Side}");

Console.WriteLine($" Area: {sq.Area}");

break;

case Rectangle r when r.Length == r.Width && r.Area > 0:

Console.WriteLine("Information about square rectangle:");

Console.WriteLine($" Length of a side: {r.Length}");

Console.WriteLine($" Area: {r.Area}");

break;

case Rectangle r when sh.Area > 0:

Console.WriteLine("Information about rectangle:");

Console.WriteLine($" Dimensions: {r.Length} x {r.Width}");

Console.WriteLine($" Area: {r.Area}");

break;

case Shape shape when sh != null:

Console.WriteLine($"A {sh.GetType().Name} shape");

break;

default:

Console.WriteLine($"The {nameof(sh)} variable does not represent a Shape.");

break;

}

}

}

// The example displays the following output:

// Information about square:

// Length of a side: 10

// Area: 100

// Information about rectangle:

// Dimensions: 5 x 7

// Area: 35

// An uninitialized shape

// The shape: Square with no dimensions

// Information about square rectangle:

// Length of a side: 8

// Area: 64

// A Circle shape

请注意,不会执行尝试测试 Shape 对象是否为 null 的示例中的 when 子句。 测试是否为 null 的正确类型模式是 case null:。

C# 语言规范

有关详细信息,请参阅 C# 语言规范中的 switch 语句。 该语言规范是 C# 语法和用法的权威资料。

请参阅

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值