tagged [yield]
C#: yield return range/collection
C#: yield return range/collection I use the `yield return` keyword quite a bit, but I find it lacking when I want to add a range to the `IEnumerable`. Here's a quick example of what I would like to do...
What's the use of yield break?
What's the use of yield break? > [What does “yield break;” do in C#?](https://stackoverflow.com/questions/231893/what-does-yield-break-do-in-c) Can anyone see a use for the "yield break" statement t...
What is the purpose/advantage of using yield return iterators in C#?
What is the purpose/advantage of using yield return iterators in C#? All of the examples I've seen of using `yield return x;` inside a C# method could be done in the same way by just returning the who...
- Modified
- 6 Jul at 18:11
Why is break required after yield return in a switch statement?
Why is break required after yield return in a switch statement? Can somebody tell me why compiler thinks that `break` is necessary after `yield return` in the following code? ``` foreach (DesignerNode...
- Modified
- 17 May at 13:38
Why can't "return" and "yield return" be used in the same method?
Why can't "return" and "yield return" be used in the same method? Why can't we use both return and yield return in the same method? For example, we can have GetIntegers1 and GetIntegers2 below, but no...
- Modified
- 9 Mar at 09:7
Why is the compiler-generated enumerator for "yield" not a struct?
Why is the compiler-generated enumerator for "yield" not a struct? The [compiler-generated implementation](http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx) of `IEnumerator`...
- Modified
- 12 Dec at 16:25
How to yield return inside anonymous methods?
How to yield return inside anonymous methods? Basically I have an anonymous method that I use for my `BackgroundWorker`: When I do this the compiler tells me: > "The yield statement cannot be used in...
- Modified
- 23 Mar at 21:8
Concurrency or Performance Benefits of yield return over returning a list
Concurrency or Performance Benefits of yield return over returning a list I was wondering if there is any concurrency (now or future), or performance benefit to using yield return over returning a lis...
- Modified
- 25 Nov at 15:20
C# yield return performance
C# yield return performance How much space is reserved to the underlying collection behind a method using yield return syntax WHEN I PERFORM a ToList() on it? There's a chance it will reallocate and t...
implementing a state machine using the "yield" keyword
implementing a state machine using the "yield" keyword Is it feasible to use the yield keyword to implement a simple state machine [as shown here](http://en.wikipedia.org/wiki/Finite_State_Machine). T...
- Modified
- 28 Jul at 15:19
Why use the yield keyword, when I could just use an ordinary IEnumerable?
Why use the yield keyword, when I could just use an ordinary IEnumerable? Given this code: Why should I not just code it this way?: ``` IEnumerable FilteredList() { var list = new List(); foreach...
How to handle an "infinite" IEnumerable?
How to handle an "infinite" IEnumerable? A trivial example of an "infinite" IEnumerable would be I know, that and both work fine
- Modified
- 29 Apr at 19:15
Using IEnumerable without foreach loop
Using IEnumerable without foreach loop I've gotta be missing something simple here. Take the following code: ``` public IEnumerable getInt(){ for(int i = 0; i iter = obj.getI
- Modified
- 12 Feb at 02:43
Thread safety of yield return with Parallel.ForEach()
Thread safety of yield return with Parallel.ForEach() Consider the following code sample, which creates an enumerable collection of integers and processes it in parallel: ``` using System.Collections....
- Modified
- 24 Nov at 08:41
Parallel.Foreach + yield return?
Parallel.Foreach + yield return? I want to process something using parallel loop like this : Ok, it works fine. But How to do if I want the FillLogs method return an IEnumerable ? ``` public IEnumerab...
- Modified
- 7 Dec at 09:38
Yielding with an IDisposable resource
Yielding with an IDisposable resource Is there a proper way to yield through a disposable resource? The returned objects are IDisposable, but the element it is iterating through is. Here is an example...
- Modified
- 22 Nov at 01:23
What is the yield keyword used for in C#?
What is the yield keyword used for in C#? In the [How Can I Expose Only a Fragment of IList](https://stackoverflow.com/questions/39447/how-can-i-expose-only-a-fragment-of-ilist) question one of the an...
C#: yield return within a foreach fails - body cannot be an iterator block
C#: yield return within a foreach fails - body cannot be an iterator block Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and `y...
Custom Collection Implementing IEnumerable
Custom Collection Implementing IEnumerable I know that technically, an Interface is used for reading and not writting or editing however, I want to add an add and addrange function to the following cl...
- Modified
- 21 May at 15:7
Recursion with yield return elements order in tree
Recursion with yield return elements order in tree I have a recursive function that returns all subtree nodes, given the starting root node. For the following tree structure: ``` A | +--B | +--C | | |...
- Modified
- 3 Feb at 10:3
Thread.Sleep or Thread.Yield
Thread.Sleep or Thread.Yield I have a method that uses a background worker to poll a DLL for a status looking something like this: ``` var timeout = DateTime.Now.AddSeconds(3); while (System.Status !=...
- Modified
- 18 Aug at 15:34
Proper use of 'yield return'
Proper use of 'yield return' The [yield](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/yield) keyword is one of those [keywords](https://learn.microsoft.com/en-us/dotnet/...
- Modified
- 24 Oct at 19:2
How is it possible to see C# code after compilation/optimization?
How is it possible to see C# code after compilation/optimization? I was reading about the `yield` keyword when I came across a sample chapter from : [http://csharpindepth.com/Articles/Chapter6/Iterato...
- Modified
- 15 Dec at 23:23
Does the C# Yield free a lock?
Does the C# Yield free a lock? I have the following method: ``` public static IEnumerable> GetRowsIter (this SqlCeResultSet resultSet) { // Make sure we don't multi thread the database. lock (Dat...
Why can you not use yield in a lambda, when you can use await in a lambda?
Why can you not use yield in a lambda, when you can use await in a lambda? [According to Eric Lippert, anonymous iterators were not added to the language because it would be overly complicated to impl...
- Modified
- 1 Jan at 13:14