10

是否可以在 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;  
 }

在上面提到的示例中,如果MyEnum = 3则必须在Year > 2012时执行... 可能吗?

[编辑]

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

4

8 回答 8

19

C#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;
}
于 2017-04-05T16:49:29.760 回答
13

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

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

另一种选择是:

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

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

于 2013-01-10T06:56:33.120 回答
4

答案是不。

您将需要以下内容:

switch (MyEnum)
{
   case 1:
   case 2:
       DoSomething();
       break;
   case 3:
       if (Year > 2012) DoSomething();
       break;
}
于 2013-01-10T06:58:47.823 回答
1

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

case 3:
      if( > 2012)
       {
       //Do Something here..........
       }
break; 
于 2013-01-10T06:50:33.877 回答
0

您必须在 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;  
 }
于 2013-01-10T06:53:04.353 回答
0

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

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;  
 }
于 2013-01-10T07:12:06.990 回答
0

大多数时候,我会做类似的事情......

(对不起命名,用例没有启发我)

考虑到那些私人课程:

private class NumberState
{
    public static NumberState GetState(int number, int year)
    {
        if (number == 1) 
            return new FirstNumberState();
        if (number == 2) 
            return new FirstNumberState();
        if (number == 3 && year > 2012) 
            return new FirstNumberState();

        if (number == 4) 
            return new SecondNumberState();
        if (number == 5) 
            return new SecondNumberState();

        return new DefaultNumberState();
    }
}

private class FirstNumberState : NumberState { }
private class SecondNumberState : NumberState { }
private class DefaultNumberState : NumberState { }

然后你可以这样做:

switch (NumberState.GetState(MyEnum, Year))
{
    case FirstNumberState _:
        // do something
        break;

    case SecondNumberState _:
        // do something
        break;

    case DefaultNumberState _:
    default:
        throw new Exception("Unhandled number state");
}

即使您的条件变得更加复杂,它也很容易阅读。

于 2020-10-06T11:50:08.173 回答
0

您可以使用 C# 8 功能

        public static bool SomeHelper(int value)
            => value switch
            {
                100 => true,
                var x when x >= 200 && x <= 300 => true,
                _ => false, // All other values
            };

您将拥有true值 100、200...300 和false其他值

您也可以使用参数:

        public static bool SomeHelper(int value, bool param)
            => value switch
            {
                100 => true,
                var x when x >= 200 && x <= 300 && param => true,
                _ => false, // All other values
            };

在这种情况下,true只有 ifvalue为 100 且param为 false

于 2021-04-20T06:49:26.603 回答