What are the real results of using the crop?

I know what yield does, and I saw a few examples, but I can’t think of real applications, did you use it to solve a specific problem?

(Ideally some kind of problem that cannot be solved in any other way)

+12
yield c #
Aug 19 '08 at 22:45
source share
7 answers

I understand that this is an old question (before John Skeet?), But I myself have been considering this issue recently. Unfortunately, the current answers here (in my opinion) do not mention the most obvious advantage of the exit report.

The biggest benefit of the yield statement is that it allows you to iterate over very large lists with much more efficient use of memory, and then use a standard list.

For example, let's say you have a database query that returns 1 million rows. You can retrieve all rows using the DataReader and store them in a list, so for this you need to specify list_size * row_size bytes of memory.

Or you can use the yield statement to create an Iterator and only ever store one line in memory at a time. In fact, this gives you the ability to provide “streaming” usage over large data sets.

Also, in code that uses Iterator, you use a simple foreach loop and you can decide to exit the loop as needed. If you break early, you are not forced to restore the entire data set when you only need the first 5 rows (for example).

Regarding

 Ideally some problem that cannot be solved some other way 

The yield statement does not give you anything you could not do using your own iterator implementation, but it eliminates the need to write often complex code. There are very few problems (if any) that cannot solve more than one way.

Here are some more recent questions and answers that provide more detailed information:

Added value to a keyword?

Is revenue useful outside LINQ?

+8
Nov 07 '09 at 11:05
source share

in fact, I use it in a non-traditional way on my IdeaPipe website

 public override IEnumerator<T> GetEnumerator() { // goes through the collection and only returns the ones that are visible for the current user // this is done at this level instead of the display level so that ideas do not bleed through // on services foreach (T idea in InternalCollection) if (idea.IsViewingAuthorized) yield return idea; } 

therefore, it basically checks to see if the idea is currently allowed to view, and if it returns the idea. If it is not, it is simply skipped. This allows me to cache ideas, but still display ideas for authorized users. Else, I would have to pull them out every time based on permissions, when they are only reviewed every 1 hour.

+5
Aug 19 '08 at 22:51
source share

One interesting use is the esp asynchronous programming mechanism for tasks that take several steps, and each step requires the same dataset. Two examples of this may be Jeffery Richters AysncEnumerator Part 1 and Part 2 . Concurrency and negotiation time (CCR) also use this method. CCR Iterators .

+2
Aug 19 '08 at 23:29
source share
The LINQ statements in the Enumerable class are implemented as iterators created using the yield statement. It allows you to chain operations like Select () and Where () without actually listing anything until you actually use an enumerator in a loop, usually using the foreach statement. In addition, since only one value is calculated when IEnumerator.MoveNext () is called, if you decide to stop the middle collection, you can save a performance hit when calculating all the results.

Iterators can also be used to implement other types of lazy evaluations, where expressions are evaluated only when you need it. You can also use output for more useful things, such as coroutines.

+1
Aug 19 '08 at 23:10
source share

Another useful advantage to exit is to execute the function on IEnumerable elements and return a result of another type, for example:

 public delegate T SomeDelegate(K obj); public IEnumerable<T> DoActionOnList(IEnumerable<K> list, SomeDelegate action) { foreach (var i in list) yield return action(i); } 
+1
Aug 19 '08 at 23:29
source share

Harvesting can prevent sinking to a specific type. This is convenient to ensure that the collection consumer does not manipulate it.

+1
Aug 19 '08 at 23:41
source share

You can also use yield return to process a series of function results in a list. For example, think of a company that pays its employees every two weeks. As a list, using this code you can get a subset of payroll dates:

 void Main() { var StartDate = DateTime.Parse("01/01/2013"); var EndDate = DateTime.Parse("06/30/2013"); foreach (var d in GetPayrollDates(StartDate, EndDate)) { Console.WriteLine(d); } } // Calculate payroll dates in the given range. // Assumes the first date given is a payroll date. IEnumerable<DateTime> GetPayrollDates(DateTime startDate, DateTime endDate, int daysInPeriod = 14) { var thisDate = startDate; while (thisDate < endDate) { yield return thisDate; thisDate = thisDate.AddDays(daysInPeriod); } } 
0
Dec 03 '13 at 22:53
source share



All Articles