In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet:
IEnumerable 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. Answer The yield keyword actually does quite a lot here.The function returns an object that implements the IEnumerable 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 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 Read(string sql, Func 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); } } } }} No comments: Post a Comment Newer Post Older Post Home Subscribe to: Post Comments (Atom) plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti... c++ faq - The Definitive C++ Book Guide and List This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm... bash - jq not parsing json with spaces I’m trying to run the following command that reads JSON from a file and formats it with jq : jq -n -r --arg m $( It produces the des... python - An attempt has been made to start a new process before the current process has finished its bootstrapping phase I am new to dask and I found so nice to have a module that makes it easy to get parallelization. I am working on a project where I was able ...
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.
Answer
The yield keyword actually does quite a lot here.
yield
The function returns an object that implements the IEnumerable 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 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 Read(string sql, Func 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); } } } }} No comments: Post a Comment Newer Post Older Post Home Subscribe to: Post Comments (Atom) plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti... c++ faq - The Definitive C++ Book Guide and List This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm... bash - jq not parsing json with spaces I’m trying to run the following command that reads JSON from a file and formats it with jq : jq -n -r --arg m $( It produces the des... python - An attempt has been made to start a new process before the current process has finished its bootstrapping phase I am new to dask and I found so nice to have a module that makes it easy to get parallelization. I am working on a project where I was able ...
IEnumerable 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 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 Read(string sql, Func 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); } } } }} No comments: Post a Comment Newer Post Older Post Home Subscribe to: Post Comments (Atom) plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti... c++ faq - The Definitive C++ Book Guide and List This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm... bash - jq not parsing json with spaces I’m trying to run the following command that reads JSON from a file and formats it with jq : jq -n -r --arg m $( It produces the des... python - An attempt has been made to start a new process before the current process has finished its bootstrapping phase I am new to dask and I found so nice to have a module that makes it easy to get parallelization. I am working on a project where I was able ...
foreach
IEnumerable
IEnumerator
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 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.
Integers()
1
2
yield return 1
Here is a real-life example:
public IEnumerable Read(string sql, Func 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); } } } }}
In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...