In specific, if I say:
public static IEnumerable<String> Data()
{
String connectionString = "...";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
IDataReader reader = new SqlCommand("", connection).ExecuteReader();
while (reader.Read())
yield return String.Format("Have a beer {0} {1}!", reader["First_Name"], reader["Last_Name"]);
connection.Close();
}
}
How does the compiler go about generating a concrete enumerable class out of this?
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.
You use a yield return statement to return each element one at a time. The sequence returned from an iterator method can be consumed by using a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method.
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.
Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator<T> for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type. The following example shows the two forms of the yield statement.
It builds a state machine, basically:
state
variable to keep track of where it's got toIEnumerable<T>
and IEnumerator<T>
- the MoveNext()
method gets to the right bit of the logic (based on state
) and sets an instance variable to keep track of the last-yielded value (the Current
property)See my article on the topic for more details. Also note that async/await in C# 5 is built with a lot of the same ideas (although there are various implementation differences).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With