代码检查:枚举的一些值未在 'switch' 语句中处理
在使用 switch 语句与 enum 时,不需要为每个枚举值都提供 case 语句——如果某些值没有对应的 case, switch 将对这些值不执行任何操作。
虽然缺少的 case 可能是作者有意为之,但更多情况下是因为向 enum 中添加了新值却忘记相应地更新 switch 所导致的。
JetBrains Rider 将此类 switch 语句标记为潜在问题,并建议为未处理的值生成 case 语句。
enum TestEnum
{
A,
B
}
class Program
{
void Test(TestEnum testEnum)
{
// case 'TestEnum.B' is not handled and won't do anything
switch (testEnum)
{
case TestEnum.A:
Console.WriteLine("A");
break;
}
}
}
最后修改日期: 2025年 9月 26日