Yield return usage in C #

Source: Internet
Author: User

The 1.yield keyword is used to traverse loops, and yield return is used to return Ienumerable<t>,yield break for terminating loop traversal

Implementations that do not use yield return

  
 
  1. static IEnumerable<int> FilterWithoutYield()
  2. {
  3. List<int> result = new List<int>();
  4. foreach (int i in GetInitialData())
  5. {
  6. if (i > 2)
  7. {
  8. result.Add(i); //这里的add就把结果都给了result,返回一个List<int>类型的result对象,所以这里返回的结果result是List<int>类型,会转化为IEnumerable<int>类型
  9. }
  10. }
  11. return result;
  12. }
use yeild return to implement
  
 
  1. static IEnumerable<int> FilterWithYield()
  2. {
  3. foreach (int i in GetInitialData())
  4. {
  5. if (i > 2)
  6. {
  7. yield return i;
  8. }
  9. }
  10. yield break;
  11. Console.WriteLine("这里的代码不执行");
  12. }
The code for the client call is the same
  
 
  1. static void Main(string[] args)
  2. {
  3. foreach (var item in FilterWithYield())
  4. {
  5. Console.WriteLine(item);
  6. }
  7. Console.ReadKey();
  8. }
using this code to test a LINQ expression, the result is that the class of the method is not a ienumerable<t> type, only the return value of the method is Ienumerable<t>, so only the return value of the method can be used with LINQSo there are two conditions for using LINQ: 1. For classes, this class inherits Ienumerable<t&gt, and it is necessary to implement IEnumerator Getnumerator method 2. For methods, this method returns ienumerable<t Types of > interfacesAlthough the output of the 2 methods is the same, the operating process is very different. The first method is to load the result set into memory and then traverse it, and the second method, each time the client calls, yields return a value to the client, which is "supply on demand".

The first method, the client invocation process is roughly:

With yield return, the client invocation process is roughly:

Why is the use of yield return guaranteed to start executing once every time the loop is traversed?

--Because the compiler generates a state machine to maintain the state of the iterator.

Simply put, when you want to get a collection of ienumerable<t> types instead of loading the data into memory at once, consider using yield return for "on demand".


Source: >
implementing the code for a IEnumerable interface
  
 
  1. public class Stack : IEnumerable
  2. {
  3. int [] items = { 1 2 3 4 5 6 7 8 9 };
  4. public IEnumerator GetEnumerator()
  5. {
  6. for (int i = 0; i < items.Length; i++)
  7. {
  8. yield return items[i]; // return返回的是IEnumerator集合对象,也就是说类中本身就有了一个
  9. //IEnumerator对象,Foreach遍历的时候,也遍历的是这个IEnumerator对象,所以直接写foreach(var i in ss)就可以了,底层帮助完成其他代码
  10. }
  11. }
  12. }





 

From for notes (Wiz)

Yield return usage in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.