What is the yield keyword used for in C#?
Asked Answered
R

20

1052

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet:

IEnumerable<object> FilteredList()
{
    foreach(object item in FullList)
    {
        if(IsItemInPartialList(item))
            yield return item;
    }
}

What does the yield keyword do there? I've seen it referenced in a couple places, and one other question, but I haven't quite figured out what it actually does. I'm used to thinking of yield in the sense of one thread yielding to another, but that doesn't seem relevant here.

Reckless answered 2/9, 2008 at 13:15 Comment(2)
Just MSDN link about it is here msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspxForegut
This is not surprising. The confusion comes from the fact that we are conditioned to see "return" as a function output while preceded by a "yield" it is not.Lole
P
953

The yield contextual keyword actually does quite a lot here.

The function returns an object that implements the IEnumerable<object> interface. If a calling function starts foreaching over this object, the function is called again until it "yields". This is syntactic sugar introduced in C# 2.0. In earlier versions you had to create your own IEnumerable and IEnumerator objects to do stuff like this.

The easiest way understand code like this is to type-in an example, set some breakpoints and see what happens. Try stepping through this example:

public void Consumer()
{
    foreach(int i in Integers())
    {
        Console.WriteLine(i.ToString());
    }
}

public IEnumerable<int> Integers()
{
    yield return 1;
    yield return 2;
    yield return 4;
    yield return 8;
    yield return 16;
    yield return 16777216;
}

When you step through the example, you'll find the first call to Integers() returns 1. The second call returns 2 and the line yield return 1 is not executed again.

Here is a real-life example:

public IEnumerable<T> Read<T>(string sql, Func<IDataReader, T> make, params object[] parms)
{
    using (var connection = CreateConnection())
    {
        using (var command = CreateCommand(CommandType.Text, sql, connection, parms))
        {
            command.CommandTimeout = dataBaseSettings.ReadCommandTimeout;
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return make(reader);
                }
            }
        }
    }
}
Piliferous answered 2/9, 2008 at 13:23 Comment(8)
In this case that would be easier, i'm just using the integer here to show how yield return works. The nice things about using yield return is that it's a very quick way of implementing the iterator pattern, so things are evaluated lazly.Piliferous
Also worth noting you can use yield break; when you don't want to return any more items.Endstopped
Any Linq Operations U use are actually Lazy Loading which is done by using yield , in fact All ORM Uses this...Wien
yield is not a keyword. If it were then I could not use yield as an identifier as in int yield = 500;Hardened
@Hardened that's because all programming languages support two types of keywords namely reserved and contextual. yield falls in the later category which is why your code is not prohibited by the C# compiler. More details here: ericlippert.com/2009/05/11/reserved-and-contextual-keywords You would be thrilled to know that there are also reserved words which are not recognized as keywords by a language. For e.g. goto in java. More details here: #2545603Blueberry
'If a calling function starts foreach-ing over this object the function is called again until it "yields"'. doesn't sound right to me. I always thought of the c# yield keyword in the context of "the crop yields a bountiful harvest", instead of "the car yields to the pedestrian".Action
Zack, in this case, return is the provider, and yield is a marker for where control flow was suspended, and control itself was relinquished. The IEnumerable yields control to the calling context/loop, like your car and pedestrian, while return carries the present state payload, the crops in your example. Yes return alone relinquishes control in a major way, but yield is like a goto that gets created at the point of return, to be followed next invocation; resuming where the last yielded control, vs abandoning it in the case of a traditional return.Lugansk
If I'm not mistaken, this is almost the same as a ref parameter in a function?Chigoe
F
444

Iteration. It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.

Faucet answered 2/9, 2008 at 13:17 Comment(0)
J
296

Yield has two great uses,

  1. It helps to provide custom iteration without creating temp collections.

  2. It helps to do stateful iteration. enter image description here

In order to explain above two points more demonstratively, I have created a simple video you can watch it here

Jesusitajet answered 12/4, 2013 at 17:31 Comment(3)
The video help me to clearly understand the yield. @ShivprasadKoirala's code project article What is the use of C# Yield ? of the same explanation is also a good sourceHypercriticism
I would also add as a third point that yield is "fast" way to create a custom IEnumerator (rather having a class implement the IEnumerator interface).Runyon
Great video, but wondering... The implementation using yield is obviously cleaner, but it must be essentially creating its own temp memory or/and List internally in order to keep track of the state (or rather creating a state machine). So, is "Yield" doing anything else than making the implementation simpler and making things look better or is there something else to it? How about efficiency, is running code using Yield more or less efficient/fast than without?Concha
Y
156

Recently Raymond Chen also ran an interesting series of articles on the yield keyword.

While it's nominally used for easily implementing an iterator pattern, but can be generalized into a state machine. No point in quoting Raymond, the last part also links to other uses (but the example in Entin's blog is esp good, showing how to write async safe code).

Yeasty answered 2/9, 2008 at 13:27 Comment(2)
This needs to be up voted. Sweet how he explains the purpose of the operator and internals.Convulse
part 1 explains the syntactic sugar of "yield return". excellent explaining!Jarrodjarrow
A
151

At first sight, yield return is a .NET sugar to return an IEnumerable.

Without yield, all the items of the collection are created at once:

class SomeData
{
    public SomeData() { }

    static public IEnumerable<SomeData> CreateSomeDatas()
    {
        return new List<SomeData> {
            new SomeData(), 
            new SomeData(), 
            new SomeData()
        };
    }
}

Same code using yield, it returns item by item:

class SomeData
{
    public SomeData() { }

    static public IEnumerable<SomeData> CreateSomeDatas()
    {
        yield return new SomeData();
        yield return new SomeData();
        yield return new SomeData();
    }
}

The advantage of using yield is that if the function consuming your data simply needs the first item of the collection, the rest of the items won't be created.

The yield operator allows the creation of items as it is demanded. That's a good reason to use it.

Affliction answered 17/1, 2015 at 14:23 Comment(0)
F
74

A list or array implementation loads all of the items immediately whereas the yield implementation provides a deferred execution solution.

In practice, it is often desirable to perform the minimum amount of work as needed in order to reduce the resource consumption of an application.

For example, we may have an application that process millions of records from a database. The following benefits can be achieved when we use IEnumerable in a deferred execution pull-based model:

  • Scalability, reliability and predictability are likely to improve since the number of records does not significantly affect the application’s resource requirements.
  • Performance and responsiveness are likely to improve since processing can start immediately instead of waiting for the entire collection to be loaded first.
  • Recoverability and utilisation are likely to improve since the application can be stopped, started, interrupted or fail. Only the items in progress will be lost compared to pre-fetching all of the data where only using a portion of the results was actually used.
  • Continuous processing is possible in environments where constant workload streams are added.

Here is a comparison between build a collection first such as a list compared to using yield.

List Example

    public class ContactListStore : IStore<ContactModel>
    {
        public IEnumerable<ContactModel> GetEnumerator()
        {
            var contacts = new List<ContactModel>();
            Console.WriteLine("ContactListStore: Creating contact 1");
            contacts.Add(new ContactModel() { FirstName = "Bob", LastName = "Blue" });
            Console.WriteLine("ContactListStore: Creating contact 2");
            contacts.Add(new ContactModel() { FirstName = "Jim", LastName = "Green" });
            Console.WriteLine("ContactListStore: Creating contact 3");
            contacts.Add(new ContactModel() { FirstName = "Susan", LastName = "Orange" });
            return contacts;
        }
    }

    static void Main(string[] args)
    {
        var store = new ContactListStore();
        var contacts = store.GetEnumerator();

        Console.WriteLine("Ready to iterate through the collection.");
        Console.ReadLine();
    }

Console Output
ContactListStore: Creating contact 1
ContactListStore: Creating contact 2
ContactListStore: Creating contact 3
Ready to iterate through the collection.

Note: The entire collection was loaded into memory without even asking for a single item in the list

Yield Example

public class ContactYieldStore : IStore<ContactModel>
{
    public IEnumerable<ContactModel> GetEnumerator()
    {
        Console.WriteLine("ContactYieldStore: Creating contact 1");
        yield return new ContactModel() { FirstName = "Bob", LastName = "Blue" };
        Console.WriteLine("ContactYieldStore: Creating contact 2");
        yield return new ContactModel() { FirstName = "Jim", LastName = "Green" };
        Console.WriteLine("ContactYieldStore: Creating contact 3");
        yield return new ContactModel() { FirstName = "Susan", LastName = "Orange" };
    }
}

static void Main(string[] args)
{
    var store = new ContactYieldStore();
    var contacts = store.GetEnumerator();

    Console.WriteLine("Ready to iterate through the collection.");
    Console.ReadLine();
}

Console Output
Ready to iterate through the collection.

Note: The collection wasn't executed at all. This is due to the "deferred execution" nature of IEnumerable. Constructing an item will only occur when it is really required.

Let's call the collection again and obverse the behaviour when we fetch the first contact in the collection.

static void Main(string[] args)
{
    var store = new ContactYieldStore();
    var contacts = store.GetEnumerator();
    Console.WriteLine("Ready to iterate through the collection");
    Console.WriteLine("Hello {0}", contacts.First().FirstName);
    Console.ReadLine();
}

Console Output
Ready to iterate through the collection
ContactYieldStore: Creating contact 1
Hello Bob

Nice! Only the first contact was constructed when the client "pulled" the item out of the collection.

Floatation answered 4/12, 2015 at 19:52 Comment(3)
This answer needs more attention! ThxLuncheonette
@Luncheonette absolutely +2Combine
There is a performance penalty, though. It makes little sense to use yield for small, in-memory lists.Petronius
M
50

yield return is used with enumerators. On each call of yield statement, control is returned to the caller but it ensures that the callee's state is maintained. Due to this, when the caller enumerates the next element, it continues execution in the callee method from statement immediately after the yield statement.

Let us try to understand this with an example. In this example, corresponding to each line I have mentioned the order in which execution flows.

static void Main(string[] args)
{
    foreach (int fib in Fibs(6))//1, 5
    {
        Console.WriteLine(fib + " ");//4, 10
    }            
}

static IEnumerable<int> Fibs(int fibCount)
{
    for (int i = 0, prevFib = 0, currFib = 1; i < fibCount; i++)//2
    {
        yield return prevFib;//3, 9
        int newFib = prevFib + currFib;//6
        prevFib = currFib;//7
        currFib = newFib;//8
    }
}

Also, the state is maintained for each enumeration. Suppose, I have another call to Fibs() method then the state will be reset for it.

Mousebird answered 25/2, 2014 at 17:15 Comment(1)
set prevFib = 1 - the first Fibonacci number is a "1", not a "0"Vantage
R
41

If I understand this correctly, here's how I would phrase this from the perspective of the function implementing IEnumerable with yield.

  • Here's one.
  • Call again if you need another.
  • I'll remember what I already gave you.
  • I'll only know if I can give you another when you call again.
Ramberg answered 15/4, 2018 at 22:28 Comment(1)
simple & brilliantMultimillionaire
Z
35

Intuitively, the keyword returns a value from the function without leaving it, i.e. in your code example it returns the current item value and then resumes the loop. More formally, it is used by the compiler to generate code for an iterator. Iterators are functions that return IEnumerable objects. The MSDN has several articles about them.

Zeist answered 2/9, 2008 at 13:19 Comment(2)
Well, to be precise it does not resume the loop, it pauses it until the parent calls "iterator.next()".Delija
@jitbit That’s why I used “intuitively” and “more formally”.Zeist
P
34

Here is a simple way to understand the concept: The basic idea is, if you want a collection that you can use "foreach" on, but gathering the items into the collection is expensive for some reason (like querying them out of a database), AND you will often not need the entire collection, then you create a function that builds the collection one item at a time and yields it back to the consumer (who can then terminate the collection effort early).

Think of it this way: You go to the meat counter and want to buy a pound of sliced ham. The butcher takes a 10-pound ham to the back, puts it on the slicer machine, slices the whole thing, then brings the pile of slices back to you and measures out a pound of it. (OLD way). With yield, the butcher brings the slicer machine to the counter, and starts slicing and "yielding" each slice onto the scale until it measures 1-pound, then wraps it for you and you're done. The Old Way may be better for the butcher (lets him organize his machinery the way he likes), but the New Way is clearly more efficient in most cases for the consumer.

Pes answered 1/9, 2016 at 13:43 Comment(0)
T
29

The yield keyword allows you to create an IEnumerable<T> in the form on an iterator block. This iterator block supports deferred executing and if you are not familiar with the concept it may appear almost magical. However, at the end of the day it is just code that executes without any weird tricks.

An iterator block can be described as syntactic sugar where the compiler generates a state machine that keeps track of how far the enumeration of the enumerable has progressed. To enumerate an enumerable, you often use a foreach loop. However, a foreach loop is also syntactic sugar. So you are two abstractions removed from the real code which is why it initially might be hard to understand how it all works together.

Assume that you have a very simple iterator block:

IEnumerable<int> IteratorBlock()
{
    Console.WriteLine("Begin");
    yield return 1;
    Console.WriteLine("After 1");
    yield return 2;
    Console.WriteLine("After 2");
    yield return 42;
    Console.WriteLine("End");
}

Real iterator blocks often have conditions and loops but when you check the conditions and unroll the loops they still end up as yield statements interleaved with other code.

To enumerate the iterator block a foreach loop is used:

foreach (var i in IteratorBlock())
    Console.WriteLine(i);

Here is the output (no surprises here):

Begin
1
After 1
2
After 2
42
End

As stated above foreach is syntactic sugar:

IEnumerator<int> enumerator = null;
try
{
    enumerator = IteratorBlock().GetEnumerator();
    while (enumerator.MoveNext())
    {
        var i = enumerator.Current;
        Console.WriteLine(i);
    }
}
finally
{
    enumerator?.Dispose();
}

In an attempt to untangle this I have crated a sequence diagram with the abstractions removed:

C# iterator block sequence diagram

The state machine generated by the compiler also implements the enumerator but to make the diagram more clear I have shown them as separate instances. (When the state machine is enumerated from another thread you do actually get separate instances but that detail is not important here.)

Every time you call your iterator block a new instance of the state machine is created. However, none of your code in the iterator block is executed until enumerator.MoveNext() executes for the first time. This is how deferred executing works. Here is a (rather silly) example:

var evenNumbers = IteratorBlock().Where(i => i%2 == 0);

At this point the iterator has not executed. The Where clause creates a new IEnumerable<T> that wraps the IEnumerable<T> returned by IteratorBlock but this enumerable has yet to be enumerated. This happens when you execute a foreach loop:

foreach (var evenNumber in evenNumbers)
    Console.WriteLine(eventNumber);

If you enumerate the enumerable twice then a new instance of the state machine is created each time and your iterator block will execute the same code twice.

Notice that LINQ methods like ToList(), ToArray(), First(), Count() etc. will use a foreach loop to enumerate the enumerable. For instance ToList() will enumerate all elements of the enumerable and store them in a list. You can now access the list to get all elements of the enumerable without the iterator block executing again. There is a trade-off between using CPU to produce the elements of the enumerable multiple times and memory to store the elements of the enumeration to access them multiple times when using methods like ToList().

Trogon answered 28/6, 2017 at 11:54 Comment(0)
D
26

One major point about Yield keyword is Lazy Execution. Now what I mean by Lazy Execution is to execute when needed. A better way to put it is by giving an example

Example: Not using Yield i.e. No Lazy Execution.

public static IEnumerable<int> CreateCollectionWithList()
{
    var list =  new List<int>();
    list.Add(10);
    list.Add(0);
    list.Add(1);
    list.Add(2);
    list.Add(20);

    return list;
}

Example: using Yield i.e. Lazy Execution.

public static IEnumerable<int> CreateCollectionWithYield()
{
    yield return 10;
    for (int i = 0; i < 3; i++) 
    {
        yield return i;
    }

    yield return 20;
}

Now when I call both methods.

var listItems = CreateCollectionWithList();
var yieldedItems = CreateCollectionWithYield();

you will notice listItems will have a 5 items inside it (hover your mouse on listItems while debugging). Whereas yieldItems will just have a reference to the method and not the items. That means it has not executed the process of getting items inside the method. A very efficient way of getting data only when needed. Actual implementation of yield can be seen in ORM like Entity Framework and NHibernate etc.

Decrepitude answered 17/10, 2019 at 23:35 Comment(1)
Looking over all the answers, this answer shows me that yield is a hack for bad design in the core of a language. The underlying problem in this case is IEnumerable and foreach. Also, the return is one item at a time. That means there will be a LOT of extra CPU overhead if every item is desired. Probably about the same level of inefficiency as returning everything at once. A better solution would be to find a balance between the two. Return chunks of 100-1000 items (or whatever is a "reasonable" amount) at a time, enumerate those, return the next chunk, etc. SQL cursors do this.Mcgregor
S
17

The C# yield keyword, to put it simply, allows many calls to a body of code, referred to as an iterator, that knows how to return before it's done and, when called again, continues where it left off - i.e. it helps an iterator become transparently stateful per each item in a sequence that the iterator returns in successive calls.

In JavaScript, the same concept is called Generators.

Seurat answered 14/1, 2013 at 22:49 Comment(1)
Best explanation yet. Are these also the same generators in python?Taxeme
C
13

It is a very simple and easy way to create an enumerable for your object. The compiler creates a class that wraps your method and that implements, in this case, IEnumerable<object>. Without the yield keyword, you'd have to create an object that implements IEnumerable<object>.

Cleat answered 2/9, 2008 at 13:17 Comment(0)
M
7

It's producing enumerable sequence. What it does is actually creating local IEnumerable sequence and returning it as a method result

Mesopause answered 2/9, 2008 at 13:18 Comment(0)
E
5

This link has a simple example

Even simpler examples are here

public static IEnumerable<int> testYieldb()
{
    for(int i=0;i<3;i++) yield return 4;
}

Notice that yield return won't return from the method. You can even put a WriteLine after the yield return

The above produces an IEnumerable of 4 ints 4,4,4,4

Here with a WriteLine. Will add 4 to the list, print abc, then add 4 to the list, then complete the method and so really return from the method(once the method has completed, as would happen with a procedure without a return). But this would have a value, an IEnumerable list of ints, that it returns on completion.

public static IEnumerable<int> testYieldb()
{
    yield return 4;
    console.WriteLine("abc");
    yield return 4;
}

Notice also that when you use yield, what you are returning is not of the same type as the function. It's of the type of an element within the IEnumerable list.

You use yield with the method's return type as IEnumerable. If the method's return type is int or List<int> and you use yield, then it won't compile. You can use IEnumerable method return type without yield but it seems maybe you can't use yield without IEnumerable method return type.

And to get it to execute you have to call it in a special way.

static void Main(string[] args)
{
    testA();
    Console.Write("try again. the above won't execute any of the function!\n");

    foreach (var x in testA()) { }


    Console.ReadLine();
}



// static List<int> testA()
static IEnumerable<int> testA()
{
    Console.WriteLine("asdfa");
    yield return 1;
    Console.WriteLine("asdf");
}
Easy answered 1/5, 2016 at 1:25 Comment(3)
note- if trying to understand SelectMany, it uses yield and also generics.. this example may help public static IEnumerable<TResult> testYieldc<TResult>(TResult t) { yield return t; } and public static IEnumerable<TResult> testYieldc<TResult>(TResult t) { return new List<TResult>(); }Easy
Looks like very good explanation ! This could have been the accepted answer.Severe
@pongapundit thanks, my answer is certainly clear and simple, but I haven't used yield much myself, other answerers have much more experience with it and knowledge of its uses than I do. What I wrote of yield here was probably from scratching my head trying to figure out some of the answers here and at that dotnetperls link! But since I don't know yield return that well (other than the simple thing I mentioned), and haven't used it much and don't know much re its uses, I don't think this should be the accepted one.Easy
E
5

Nowadays you can use the yield keyword for async streams.

C# 8.0 introduces async streams, which model a streaming source of data. Data streams often retrieve or generate elements asynchronously. Async streams rely on new interfaces introduced in .NET Standard 2.1. These interfaces are supported in .NET Core 3.0 and later. They provide a natural programming model for asynchronous streaming data sources.

Source: Microsoft docs

Example below

using System;
using System.Collections.Generic;               
using System.Threading.Tasks;

public class Program
{
    public static async Task Main()
    {
        List<int> numbers = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        
        await foreach(int number in YieldReturnNumbers(numbers))
        {
            Console.WriteLine(number);
        }
    }
    
    public static async IAsyncEnumerable<int> YieldReturnNumbers(List<int> numbers) 
    {
        foreach (int number in numbers)
        {
            await Task.Delay(1000);
            yield return number;
        }
    }
}
Exhibitioner answered 6/11, 2021 at 18:23 Comment(0)
C
0

After reading all posts, I did create "yield return" concept for myself. First: "return" exits Function & gets back to caller with result. Second: "yield" remembers state of function (loop step or exit place), so next Function call continues loop, omitting initialization or already executed statements.

Cana answered 31/8, 2023 at 18:19 Comment(0)
P
-3

Simple demo to understand yield

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp_demo_yield {
    class Program
    {
        static void Main(string[] args)
        {
            var letters = new List<string>() { "a1", "b1", "c2", "d2" };

            // Not yield
            var test1 = GetNotYield(letters);

            foreach (var t in test1)
            {
                Console.WriteLine(t);
            }

            // yield
            var test2 = GetWithYield(letters).ToList();

            foreach (var t in test2)
            {
                Console.WriteLine(t);
            }

            Console.ReadKey();
        }

        private static IList<string> GetNotYield(IList<string> list)
        {
            var temp = new List<string>();
            foreach(var x in list)
            {
                
                if (x.Contains("2")) { 
                temp.Add(x);
                }
            }

            return temp;
        }

        private static IEnumerable<string> GetWithYield(IList<string> list)
        {
            foreach (var x in list)
            {
                if (x.Contains("2"))
                {
                    yield return x;
                }
            }
        }
    } 
}
Percolator answered 1/10, 2021 at 17:51 Comment(2)
I think some explanation is needed to add to this answer to clarifying what you mean.Calciferous
I don't like this example because ToList causes immediate evaluation of the query thereby defeating the purpose of using 'yield'.Seducer
C
-5

It's trying to bring in some Ruby Goodness :)
Concept: This is some sample Ruby Code that prints out each element of the array

 rubyArray = [1,2,3,4,5,6,7,8,9,10]
    rubyArray.each{|x| 
        puts x   # do whatever with x
    }

The Array's each method implementation yields control over to the caller (the 'puts x') with each element of the array neatly presented as x. The caller can then do whatever it needs to do with x.

However .Net doesn't go all the way here.. C# seems to have coupled yield with IEnumerable, in a way forcing you to write a foreach loop in the caller as seen in Mendelt's response. Little less elegant.

//calling code
foreach(int i in obCustomClass.Each())
{
    Console.WriteLine(i.ToString());
}

// CustomClass implementation
private int[] data = {1,2,3,4,5,6,7,8,9,10};
public IEnumerable<int> Each()
{
   for(int iLooper=0; iLooper<data.Length; ++iLooper)
        yield return data[iLooper]; 
}
Czardom answered 2/9, 2008 at 14:6 Comment(2)
-1 This answer does not sound right to me. Yes, C# yield is coupled with IEnumerable, and C# lacks the Ruby concept of a "block". But C# has lambdas, which could allow the implementation of a ForEach method, much alike Ruby's each. This that does not mean it would be a good idea to do so, though.Calaboose
Better yet: public IEnumerable<int> Each() { int index = 0; yield return data[index++]; }Depopulate

© 2022 - 2024 — McMap. All rights reserved.