Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the yield keyword used for in C#?

Tags:

yield

c#

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.

like image 896
Herms Avatar asked Sep 02 '08 13:09

Herms


People also ask

What is the yield keyword used for?

The yield keyword is use to do custom stateful iteration over a collection. The yield keyword tells the compiler that the method in which it appears is an iterator block. yield return <expression>; yield break; The yield return statement returns one element at a time.

What is the benefit of yield C#?

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 so it's more efficient. The yield operator allows the creation of items as it is demanded. That's a good reason to use it.

What is a yield return?

Yield is the income returned on an investment, such as the interest received from holding a security. The yield is usually expressed as an annual percentage rate based on the investment's cost, current market value, or face value.

What is the difference between yield and return in C#?

In a normal (non-iterating) method you would use the return keyword. But you can't use return in an iterator, you have to use yield break . In other words, yield break for an iterator is the same as return for a standard method. Whereas, the break statement just terminates the closest loop.


2 Answers

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);                 }             }         }     } } 
like image 181
Mendelt Avatar answered Sep 19 '22 06:09

Mendelt


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.

like image 38
Joel Coehoorn Avatar answered Sep 21 '22 06:09

Joel Coehoorn