C# 为 Switch 中的 Case 语句添加附加条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14252293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:10:44  来源:igfitidea点击:

Add a additional condition to Case Statement in Switch

c#switch-statement

提问by andy

Is it possible to add a additional Condition to Switch Statement like below in C#

是否可以在 C# 中向 Switch 语句添加额外的条件,如下所示

switch(MyEnum)
{ 
  case 1:
  case 2:
  case 3 && Year > 2012://Additional Condtion Here
       //Do Something here..........
      break; 
  case 4:
  case 5:
       //Do Something here..........
      break;  
 }

In above mentioned example if MyEnum = 3then it has to be excuted if Year > 2012... Is it possible?

在上面提到的例子中,如果MyEnum = 3那么它必须在Year > 2012 时被执行......这可能吗?

[EDITED]

[编辑]

Year > 2012 is not applicable to case 1 & case 2.

年份 > 2012 不适用于案例 1 和案例 2。

采纳答案by JLRishe

In order for it to work the way you've indicated with the fallthrough logic for 1 and 2, I'd suggest moving the //do something hereportion out to a method or function and then doing this:

为了让它按照您使用 1 和 2 的失败逻辑指示的方式工作,我建议将//do something here部分移出方法或函数,然后执行以下操作:

  case 1:
  case 2:
      DoSomething();
      break;
  case 3:
      if(Year > 2012) { DoSomething(); }
      break; 

The other alternative would be:

另一种选择是:

  case 1:
  case 2:
  case 3:
      if (MyEnum != 3 || Year > 2012) {
         // Do something here
      }
      break; 

but I think the first option is much more intuitive and readable.

但我认为第一个选项更直观和可读。

回答by Habib

You can't add condition to a case. Case clause has to be a compile time constant. Instead you can use an if statement inside the case statement.

您不能向案例添加条件。Case 子句必须是编译时常量。相反,您可以在 case 语句中使用 if 语句。

case 3:
      if( > 2012)
       {
       //Do Something here..........
       }
break; 

回答by Rohit Vyas

You have to use the if condition inside your case , you can't use && in case statement, use like below:

你必须在你的 case 中使用 if 条件,你不能在 case 语句中使用 && ,使用如下:

switch(MyEnum)
{ 
  case 1:
  case 2:
  case 3: //Additional Condtion Here
  if (Year > 2012)
  {     
      //Do Something here..........
  }
  break; 
  case 4:
  case 5:
       //Do Something here..........
      break;  
 }

回答by jaket

The answer is no.

答案是不。

You'll need the following:

您将需要以下内容:

switch (MyEnum)
{
   case 1:
   case 2:
       DoSomething();
       break;
   case 3:
       if (Year > 2012) DoSomething();
       break;
}

回答by Mr Lister

Or, alternatively, you can add the condition to the switch:

或者,您也可以将条件添加到开关中:

switch (MyEnum!=3 || Year>2012 ? MyEnum : -1)
{ 
  case 1:
  case 2:
  case 3:
       //Do Something here..........
      break; 
  case 4:
  case 5:
       //Do Something here..........
      break;  
 }

回答by eMeL

C#7 new feature:

C#7 新特性:

case...when

情况……什么时候

https://docs.microsoft.com/hu-hu/dotnet/articles/csharp/whats-new/csharp-7

https://docs.microsoft.com/hu-hu/dotnet/articles/csharp/whats-new/csharp-7

public static int DiceSum4(IEnumerable<object> values)
{
    var sum = 0;
    foreach (var item in values)
    {
        switch (item)
        {
            case 0:
                break;
            case int val:
                sum += val;
                break;
            case IEnumerable<object> subList when subList.Any():
                sum += DiceSum4(subList);
                break;
            case IEnumerable<object> subList:
                break;
            case null:
                break;
            default:
                throw new InvalidOperationException("unknown item type");
        }
    }
    return sum;
}