【问题标题】:What is the exact usage of Yield Keyword [duplicate]Yield Keyword的确切用法是什么[重复]
【发布时间】:2014-03-21 08:45:05
【问题描述】:

在下面的代码中,我创建了一种使用 Yield 打印值的方法,另一种只是简单的列表迭代。所以在这里我的问题是从两种方法获得的输出是相同的,那么为什么我们要使用 Yield 关键字呢?并请告诉我 Yield 关键字在应用程序中的确切用法。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YieldKeyWordEg
{
    class Program
    {
        static void Main(string[] args)
        {
            Animals obj = new Animals();
            Console.WriteLine("------ Using Yield ------");
            foreach (var item in obj.GetName())
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("------ Using List Iteration ------");
            foreach (var items in obj.GetNameinList())
            {
                Console.WriteLine(items);
            }
            Console.ReadLine();
        }
    }
    class Animals
    {
        public IEnumerable<string> GetName()
        {
            List<string> objList = new List<string>();
            objList.Add("Cow");
            objList.Add("Goat");
            objList.Add("Deer");
            objList.Add("Lion");
            foreach (var item in objList)
            {
                yield return item;
            }
        }
        public List<string> GetNameinList()
        {
            List<string> objList = new List<string>();
            objList.Add("Cow");
            objList.Add("Goat");
            objList.Add("Deer");
            objList.Add("Lion");
            return objList;
        }
    }
}

输出:

------ Using Yield ------
Cow
Goat
Deer
Lion
------ Using List Iteration ------
Cow
Goat
Deer
Lion

【问题讨论】:

    标签: c# asp.net .net yield-return


    【解决方案1】:

    直接来自MSDN

    您使用 yield return 语句一次返回每个元素。当在迭代器方法中到达 yield return 语句时,将返回表达式,并保留代码中的当前位置。下次调用迭代器函数时,将从该位置重新开始执行。

    编辑: 详情见this系列文章。

    【讨论】:

    • 您能否展示 yield 的任何实际用法或与此类用法的任何链接?
    • 谢谢。我还发现这个链接可以充分理解产量的实际用法:Click
    【解决方案2】:

    使用yield return 来包装已经是 IEnumerable 的东西是一种浪费。

    yield return 可用于将 not IEnumerable 的东西包装成 IEnumerable。

    比如这样:

    public IEnumerable<string> GetNames()
    {
        yield return "Cow";
        yield return "Goat";
        yield return "Lion";
        yield return "Deer";
    }
    

    或者一些真正有意义的东西,比如a tree traversal

    【讨论】:

      【解决方案3】:

      例子:

       public IEnumerable<MyClass> GetList()
       {
           foreach (var item in SomeList)
           {
                yield return new MyClass();
           }
       }
      

      【讨论】:

        【解决方案4】:

        第二个例子中的列表什么都不做——你不返回它,当它在方法结束时超出范围时,它会被垃圾回收。在这个例子中,yield 的惯用用法是:

        public IEnumerable<string> GetName()
                {
                    yield "Cow";
                    yield "Goat";
                    yield "Deer";
                    yield "Lion";
                }
        

        【讨论】:

          猜你喜欢
          • 2012-01-07
          • 1970-01-01
          • 1970-01-01
          • 2015-03-16
          • 2012-12-07
          • 1970-01-01
          • 2015-08-12
          • 1970-01-01
          • 2021-05-25
          相关资源
          最近更新 更多