【问题标题】:c# - is there a terser way to check if a variable is one of multiple things?c# - 是否有更简洁的方法来检查变量是否是多个事物之一?
【发布时间】:2018-03-27 09:47:14
【问题描述】:

所以目前我正在这样做:

if(variable == thing1 || variable == thing2 || variable == thing3)

但这不是超级可读的。我想做的是这样的:

if(variable == thing1 || thing2 || thing3)

c#中是否存在这样的语法?

【问题讨论】:

  • 两种语法都存在,但做的事情完全不同。
  • 不要为了简短而牺牲清晰度。话虽如此,下面已经有一些很好的答案。只要确保最重要的是它更容易为下一个家伙/gal阅读,而不是更难。

标签: c# if-statement var


【解决方案1】:

你可以这样做:

int[] aux=new int[]{1,2,3}; 
if(Array.contains(aux, value))

【讨论】:

    【解决方案2】:

    将测试字符串放入列表或数组中并调用Contains.:

    var testers = new [] { "foo1", "foo2" };
    
    if (testers.Contains("subject"))
    {
       // test succeeded
    } 
    

    作为替代方案:

    if (new [] {"foo1", "foo2"}.Contains("subject"))
    {
       // test succeeded
    } 
    

    【讨论】:

      【解决方案3】:

      如果你把你所有的东西都放在一个集合中,那么是的,你可以使用 LINQ 和Any

      https://msdn.microsoft.com/en-us/library/system.linq.enumerable.any(v=vs.110).aspx

      【讨论】:

      • 如果是 Linq,为什么不Contains
      • 为什么不是 1,000,000 种方式中的任何一种,包括提交替换的代码?
      【解决方案4】:

      如果简洁的语法对你很重要,你可以定义一个扩展方法:

      public static class ObjectExtensions
      {
          public static bool In<T>(this T item, params T[] elements)
          {
              return elements.Contains(item);
          }
      }
      

      然后你可以像这样使用它:

      if (variable.In(thing1, thing2, thing3))
      

      也就是说,如果要检查的列表不会改变,我更愿意将其声明为静态只读字段,并为此调用Contains。上述扩展方法可能会导致每次调用时都会分配一个新数组,这会损害紧密循环中的性能。

      private static readonly Thing[] _things = new [] { thing1, thing2, thing3 };
      
      public void ProcessThing(Thing variable)
      {
          if (_things.Contains(variable))
          {
              // ...
          }
      }
      

      此外,如果要检查的列表包含多个项目,请改用HashSet&lt;T&gt;

      【讨论】:

      • 哇...语法不错...正如 OP 所要求的 :-)
      • 是否有理由不允许将其作为 OP 的伪代码?
      • @CamiloTerevinto:这是语言设计的问题。需要考虑一些因素,例如类型转换、短路、运算符关联性等。如果有的话,我更倾向于使用带有in 关键字的类似 SQL 的语法,这与上述扩展方法非常相似。
      【解决方案5】:

      有些人更喜欢扩展方法:

      public static bool IsOneOf<T>(this T self, params T[] values) => values.Contains(self);
      

      或类似的。

      那么你可以说:

      if (variable.IsOneOf(thing1, thing2, thing3))
      

      糟糕,我看到道格拉斯是第一个采用这种方法的人。

      它隐式使用T的默认相等比较器。

      缺点是您为所有类型创建了扩展方法。如果您只需要它,例如string,你当然可以创建一个不太通用的扩展方法。

      【讨论】:

        【解决方案6】:

        你有几个选择。

        1. 使用switch(如果thing1-thing3是常量表达式)

          switch variable
              case thing1:
              case thing2:
              case thing3:
                  DoSomething();
                  break;
          
        2. 使用正则表达式(仅适用于字符串)

          if (RegEx.Match(variable, "^(thing1|thing2|thing3)"))
          {
              DoSomething();
          }
          
        3. 使用数组

          string[] searchFor = new string[] {thing1, thing2, thing3};
          if (searchFor.Contains(variable))
          {
              DoSomething();
          }
          

        【讨论】:

          【解决方案7】:

          这对我来说很好看。如果变量 = 479 或 482 或 1482 等。

          if (new int[] { 479, 482, 1482, 2760 }.Contains(variable))
          {
              DoSomething();
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-08-08
            • 1970-01-01
            • 2012-08-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-20
            相关资源
            最近更新 更多