【发布时间】:2021-12-24 10:32:20
【问题描述】:
我是 C# 初学者。为什么在“case 6:”行出现错误(Compiler Error CS0152, A label was repeat in a switch statement.switch statement contains multiple case with the value of tag '6'),当我写这段代码时:
namespace ConsoleApp1
{
class Class1
{
public int abc { get; set; }
public Class1()
{
}
public void mTest(int aVal)
{
switch(aVal)
{
case (2 | 4):
{
break;
}
case 5:
{
break;
}
case 6:
{
break;
}
}
}
}
}
【问题讨论】:
-
2 | 4是等于6的位运算。6有两种情况,这是不允许的。如果你想测试 2 或 4,你必须写case 2: case 4: your_code; break;(多行)。
标签: c# switch-statement