【问题标题】:Why can't I conditionally add to a list this way?为什么我不能以这种方式有条件地添加到列表中?
【发布时间】:2011-08-13 07:08:15
【问题描述】:

我很确定 Java 可以让你这样做,但我可能错了。

List<string> myList = new List<string>();
bool istrue = false;
myList.Add(istrue ? "something" : void);

【问题讨论】:

    标签: c# operators


    【解决方案1】:

    你不能这样使用void。它不是表达式,它是方法 sigs 中使用的关键字。 ?: 运算符要求所有操作数都是表达式。而且我更确定你不能在 Java 中做到这一点。

    为什么不用 if 语句?它让你想做什么更清楚,正是因为void 在这种情况下毫无意义。

    1. 只在istrue时添加一些东西,否则什么都不做:

      List<string> myList = new List<string>();
      bool istrue = false;
      
      if (istrue)
      {
          myList.Add("something");
      }
      

      一行:

      if (istrue) myList.Add("something");
      
    2. 如果istrue,则添加一些内容,否则添加空值:

      List<string> myList = new List<string>();
      bool istrue = false;
      
      if (istrue)
      {
          myList.Add("something");
      }
      else
      { 
          myList.Add(null);
      }
      

      在一行中(null?: 运算符一起使用):

      myList.Add(istrue ? "something" : null);
      

    【讨论】:

    • 不优雅。我可以: myList.Add("something") if isTrue;
    • +1 表示 if 语句:if(istrue) myList.Add("something"); // 简洁明了
    • @Matthew:如 Mark 所示,您可以随时放下大括号,将所有内容放在一行中。
    【解决方案2】:

    void 仅在 (1) 作为方法的返回类型,或 (2) 作为指针类型的基础类型是合法的。 (感谢@Eric Lippert)。

    这段代码甚至无法编译。

    【讨论】:

    • 为了解决这个问题,请使用 null 而不是 void。
    • 这不会是相同的行为。问题不在于添加字符串或 null,而是在 Add 调用中添加或不添加表达式。
    • @Kyle,这实际上会添加值 null。我想要一个“中止”选项。
    • 在该三元组中,您将无法中止。这需要两行代码。
    • void 也是一种类型。它是一种仅合法的类型(1)作为方法的返回类型,或(2)作为指针类型的基础类型。因此,我必须说,这是一种非常糟糕的类型。但它是一种类型。是否是类型无关紧要;为了在条件表达式中使用,它必须是一个作为操作数有效的表达式,并且类型不是作为除“typeof”之外的任何操作数有效的表达式。
    【解决方案3】:

    这个怎么样

    public static void AddIf<T>(this List<T> list, bool flag, T obj)
    {
        if (flag) { list.Add(obj); }
    }
    

    只需传入标志和项目

    myList.AddIf(true, item);
    myList.AddIf(item!=item2, item2);
    

    【讨论】:

      【解决方案4】:

      我很高兴这不起作用。如果我调用 .Add 我希望添加一些东西。如果这种类型的逻辑在你看不到的方法中怎么办?

      myList.Add( aBlackBox.Method() );
      

      是加了还是没加?!

      【讨论】:

      • 我不明白,aBlackBox.Method() 有返回类型,不是吗?您会知道是否基于此添加了某些内容。
      • 除非调用的方法看起来像 "return istrue ? "something" : void"
      【解决方案5】:

      我很确定 Java 不会让你这样做。 void 关键字表示方法不返回任何内容。它和null不一样,也不和空列表一样,也不能用它来表示“什么都不做”,这就是你在这里的意思。

      【讨论】:

      • 你可能是对的,我已经有一段时间没有使用 Java 了。不过,我希望有一种方法可以“中止”表达式。
      【解决方案6】:

      void 不等于 NULL,

      试试

      List<string> myList = new List<string>();
      bool istrue = false;
      myList.Add(istrue ? "something" : null);
      

      【讨论】:

        【解决方案7】:

        你可以

        List<string> myList = new List<string>();
        myList.Add(false ? "true" : "false");
        

        将“false”添加到列表中。

        我猜你上面的代码在添加 void 作为字符串时遇到了问题。

        试试

        myList.Add(istrue ? "something" : string.empty);
        

        编辑 如果您只想在某些事情为真时添加,那么就这样做

        if(isTrue)
           myList.Add("something");
        

        【讨论】:

        • 我的目标是如果 isTrue 评估为假,则实际上不添加任何内容。基本上,我希望 void 是一种表达方式。
        • null 会将值 null 添加到列表中。我想要一个“中止”。但在 c# 中不存在(据我所知)。
        【解决方案8】:

        据我了解,您正在尝试有条件地添加元素作为对象初始化程序的一部分,而不是在后续添加中零碎添加。我在同一条船上。

        这是 Google 的最高搜索结果,所以为了后代......

        只要集合中每个元素的条件相同:

        var myList = (from s in new List<string> { "alpha", string.Empty, "gamma" }
                      where !String.IsNullOrWhiteSpace(s)
                      select s);
        

        【讨论】:

          【解决方案9】:

          你可以创建你的扩展方法:

          public static void Add<T>(this IList<T> source, T item, Func<bool> predicate)
          {
              if (predicate())
              {
                  source.Add(item);
              }
          }
          

          你这样使用它:

          myList.Add("something", () => null != somethingElse);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-07
            • 1970-01-01
            • 1970-01-01
            • 2019-05-26
            • 2023-04-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多