目录
一、关于switch 语句
1.switch 语句及语法结构
c# 中的 switch 语句是一种控制结构,允许您将一个表达式或值与多个值进行比较,并根据匹配的结果执行不同的代码部分。它的语法如下:
switch(expression)
{
case value1:
// 如果 expression 等于 value1,则执行此代码部分
break;
case value2:
// 如果 expression 等于 value2,则执行此代码部分
break;
// 更多的情况...
default:
// 如果 expression 不匹配任何一个情况,则执行此代码部分
break;
}
2.switch 语句的一些重要注意事项
- switch 语句中的每个 case 必须具有唯一的值。
- case 值可以是常量表达式或枚举成员。
- 可以在 switch 语句中使用 break、continue、return 或 throw 语句来退出当前的代码块。
- 如果在 switch 语句中没有指定 default 情况,则最后一个 case 之后的代码将作为默认情况执行。
- switch 语句可以用于任何标量类型,包括字符串。
- 如果在先使用了return关键字,再后使用 break关键字,则会出现警告:
严重性 | 代码 | 说明 | 项目 | 文件 | 行 | 禁止显示状态 |
警告 | cs0162 | 检测到无法访问的代码 | calcbyclass | d:\mr\05\114\calcbyclass\calcbyclass\form1.cs | 148 | 活动 |
无法访问到达break 语句,因为它出现在return 语句之后。return 语句结束封闭的 case 分支。
解决办法:删除在后使用的关键字 break关键字。
3.通常情况下的示例
1.示例1
// switch 语句
namespace _114_1
{
internal class program
{
private static void main(string[] args)
{
argumentnullexception.throwifnull(args);
int x = 5;
switch (x)
{
case 1:
console.writeline("x 等于 1");
break;
case 2:
console.writeline("x 等于 2");
break;
case 3:
console.writeline("x 等于 3");
break;
default:
console.writeline("x 不等于 1、2 或 3");
break;
}
}
}
}
//
/*
x 不等于 1、2 或 3
*/
2.示例2
// switch、case、return、break
namespace _114_2
{
internal class program
{
private static void main(string[] args)
{
argumentnullexception.throwifnull(args);
int i = 23;
int j = 17;
string type = "*";
string temp;
temp = ccount.sum(i, j, type).tostring();
console.writeline("{0} {1} {2} = {3}",i, type, j, temp);
}
}
public class ccount
{
public static int sum(int a, int b, string type)
{
switch (type) // .net8.0以上会提示ide0066,使用switch表达式警告
{
case "+":
return a + b; /*break; */
case "-":
return a - b; /*break; */
case "*":
return a * b; /*break; */
case "/":
return a / b; /*break; */
default:
return 0; /*break; */
}
}
}
}
//运行结果:
/*
23 * 17 = 391
*/
二、使用 => 运算符的 switch 表达式
1.关于switch表达式
在 c# 8.0 中引入的 switch 表达式,而不是 switch 语句,vs2019以上版本。
csharp_style_prefer_switch_expression
属性 | 值 | 说明 |
选项名称 | csharp_style_prefer_switch_expression |
|
选项值 | true | 首选使用switch表达式 |
| false | 首选使用switch语句 |
默认选项值 | true |
|
2.示例1
将上述示例2转换为使用=> 运算符的 switch表达式。
这是一个使用 => 运算符的 switch 语句的示例,它将根据提供的运算符执行不同的算术运算。
这个 switch 语句接受两个参数 a 和 b,以及一个运算符,然后根据运算符执行相应的算术运算。每个 case 语句使用 => 运算符将运算符和要执行的算术运算关联起来。
例如,如果运算符是 “+”,则执行 a + b 并返回结果。如果运算符是 “-”,则执行 a - b 并返回结果。同样,对于 “*” 和 “/”,将执行相应的乘法和除法运算。
如果提供的运算符不是 switch 语句中定义的任何一个,则执行 default 代码部分,并返回 0。
// 使用=> 运算符的 switch表达式
namespace _114_3
{
internal class program
{
private static void main(string[] args)
{
argumentnullexception.throwifnull(args);
int i = 23;
int j = 17;
string type = "*";
string temp;
temp = ccount.sum(i, j, type).tostring();
console.writeline("{0} {1} {2} = {3}", i, type, j, temp);
}
}
public class ccount
{
public static int sum(int a, int b, string type)
{
return type switch
{
"+" => a + b,
"-" => a - b,
"*" => a * b,
"/" => a / b,
_ => 0,
};
}
}
}
//运行结果:
/*
23 * 17 = 391
*/
3.示例2
该方法使用ccount
类的result
方法。result
方法使用=>运算符的switch表达式返回相应的结果字符串。
// 使用=> 运算符的return () switch的表达式
namespace _114_4
{
internal class program
{
private static void main(string[] args)
{
argumentnullexception.throwifnull(args);
int input = 4;
console.writeline(ccount.result(input)); // 输出 "four"
}
}
public class ccount
{
internal static string result(int value)
{
return (value) switch
{
1 => "one",
2 => "two",
3 => "three",
4 => "four",
_=> "default",
};
}
}
}
发表评论