2

In the below code I have created one method to print values using Yield and another just a simple list iteration. So here my question is the output obtained from both the methods are same so why do we go for Yield keyword? and also please tell me the exact usage of Yield keyword in application.

Code:

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;
        }
    }
}

Output:

------ Using Yield ------
Cow
Goat
Deer
Lion
------ Using List Iteration ------
Cow
Goat
Deer
Lion
Vignesh
  • 1,458
  • 12
  • 31
  • 59

4 Answers4

4

Straight from MSDN

You use a yield return statement to return each element one at a time.When a yield return statement is reached in the iterator method, expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterator function is called.

EDIT: To get the details see this series of articles.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • Can you show any practical usage of yield or any link to such usage? – szpic Feb 20 '14 at 12:09
  • Thanks. I also find this link to be use full understanding practical usage of yield: [Click](http://programmers.stackexchange.com/questions/97295/a-practical-use-of-yield-keyword-in-c) – szpic Feb 20 '14 at 12:15
2

It is a waste to use yield return to wrap something that is already an IEnumerable.

yield return can be used to wrap something not IEnumerable into an IEnumerable.

Such as this:

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

Or something that actually makes sense, such as a tree traversal.

Community
  • 1
  • 1
Medinoc
  • 6,577
  • 20
  • 42
0

Example:

 public IEnumerable<MyClass> GetList()
 {
     foreach (var item in SomeList)
     {
          yield return new MyClass();
     }
 }
thomas
  • 1,399
  • 1
  • 17
  • 32
0

The list in the second example does nothing - you don't return it and it'll be garbage collected when it goes out of scope at the end of the method. The idiomatic use of yield in this example is:

public IEnumerable<string> GetName()
        {
            yield "Cow";
            yield "Goat";
            yield "Deer";
            yield "Lion";
        }
Tom W
  • 5,108
  • 4
  • 30
  • 52