代码之家  ›  专栏  ›  技术社区  ›  Rob

返回空集合

  •  5
  • Rob  · 技术社区  · 17 年前

    查看此测试:

    [TestFixture]
    public class Quick_test
    {
       [Test]
       public void Test()
       {
          Assert.AreEqual(0, GetByYield().Count());
          Assert.AreEqual(0, GetByEnumerable().Count());
       }
    
       private IEnumerable<string> GetByYield()
       {
          yield break;
       }
    
       private IEnumerable<string> GetByEnumerable()
       {
          return Enumerable.Empty<string>();
       }
    }
    

    当我编写存根方法时,我通常使用Enumerable.Empty方法。我偶然发现了一些我用屈服方式编写的旧代码。

    这让我想知道:

    • 有没有什么隐藏的陷阱会让我们更喜欢其中一个?

    3 回复  |  直到 17 年前
        1
  •  6
  •   Matthew Ruston    17 年前

    我更喜欢任何能给开发者带来最清晰含义的方法。就我个人而言,我甚至不知道 屈服断裂;

        2
  •  4
  •   Amy B    17 年前
        3
  •  1
  •   leppie    17 年前

    T[] e = {};
    return e;