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

什么是收益率?在asp.NET中使用收益率有什么好处?

  •  19
  • Vijjendra  · 技术社区  · 16 年前

    你能帮助我理解这个问题吗 yield asp .NET(C#)

    5 回复  |  直到 8 年前
        1
  •  16
  •   Community Mohan Dere    9 年前

    Yield return会自动为您创建一个枚举器。

    http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx

    所以你可以这样做

    //pseudo code:
    
    while(get_next_record_from_database)
    {
      yield return your_next_record;
    }
    

    它允许您快速创建一个对象集合(枚举器),您可以在其中循环并检索记录。yield return语句处理为您创建枚举器所需的所有代码。

    yield return语句的主要部分是,在将集合返回给调用方法之前,不必加载集合中的所有项。它允许 lazy loading

    何时使用 Yield Return .

        2
  •  5
  •   John Farrell    16 年前

    想了解更多信息,我可以查看justinetherage的博客 has a great article explaining more advanced usages of yield .

        3
  •  1
  •   spoulson    16 年前

    yield 用作返回 IEnumerable<T> IEnumerator<T> 对象,而无需实现实现这些接口的自己的类。

        4
  •  1
  •   hemme    16 年前
        5
  •  0
  •   48klocs    16 年前

    yield 允许您在通常返回更具体类型(如IList)的地方发出IEnumerable。

    This is a pretty good example of how it can simplify your code and clarify your intent . 至于在哪里使用它,在页面上需要迭代集合的任何地方,都可以使用返回IEnumerable的方法来代替List/Dictionary/etc。

    推荐文章