When to use yield return, cf

Source: Internet
Author: User

When to use yield return, cf

The yield keyword is used to traverse the loop, the yield return is used to return IEnumerable <T>, and the yield break is used to terminate the loop traversal.

 

There is a set of int types:

static List<int> GetInitialData()        {            return new List<int>(){1,2,3,4};        }

Print all elements with a value greater than 2.

 

Implementation of yield return is not used

static IEnumerable<int> FilterWithoutYield()        {            List<int> result = new List<int>();            foreach (int i in GetInitialData())            {                if (i > 2)                {                    result.Add(i);                }             }            return result;        }

Client call:

static void Main(string[] args)        {            foreach (var item in FilterWithoutYield())            {                Console.WriteLine(item);            }            Console.ReadKey();         }

Output result: 3, 4

 

Implementation Using yeild return

Static IEnumerable <int> FilterWithYield () {foreach (int I in GetInitialData () {if (I> 2) {yield return I ;}} yield break; Console. writeLine ("code not executed here ");}

Client call:

static void Main(string[] args)        {            foreach (var item in FilterWithYield())            {                Console.WriteLine(item);            }            Console.ReadKey();         }

Output result: 3, 4

 

Summary

Through one-step debugging, we found that:

Although the output results of the two methods are the same, the operation process is quite different. The first method is to load all the result sets to the memory and traverse them. The second method is to return a value to the client every time the client calls yield return, which is "supplied on demand ".


In the first method, the client call process is roughly as follows:

 

When yield return is used, the client call process is roughly as follows:

 

Why can I use yield return to ensure that execution starts from the previous stopped place during each loop traversal?

-- The compiler generates a state machine to maintain the iterator state.

 

To put it simply, if you want to obtain a set of IEnumerable <T> types instead of loading data to the memory at a time, you can use yield return to implement "On-Demand Supply ".


In C language ^ how to use a1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is the same as 0 and the bitwise value is the same as 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
The above two circuits, two switches a and B, respectively. The opening status is \ [1]. The closing status is/[0].
If both are enabled or disabled, both circuits are connected.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, when circuit a and circuit B are in the same State, the power is [0], a, and circuit B are powered on at the same time [1].
C Language & |! What are the variables used by the & access operator?
Compared with the definition of variable compilation, the system will allocate space in memory.
Space memory location address & extracted address
E. g int a; the address ratio allocated to the database during compilation is 2000; & a2000
False first defines the integer pointer variable pp = & a; assigns address a 2000 to p to run p = 2000
Scanf ("% d", & a); when entering 3, the system will first find the memory space of a from the address according to & a, and then write 3 into the space.
* The pointer operator is used to retrieve variable values based on the variable address.
Compare to * a value variable a value 3
The following is a summary of definitions and declarations using pointers.
Int * p; Define pointer to integer data
Int * p [n]; the defined pointer array p is composed of n elements pointing to the Integer Data Pointer.
Int (* p) [n]; p points to the array pointer variable containing the nelement dimension
Int * p (); p returns the pointer function. This Pointer Points to integer data.
Int (* p) (); p points to the function pointer. This function returns an integer value.
Int ** p; p pointer variable pointing to Integer Data Pointer variable
For more information about the system, see Tan haoqiang's c Programming (the third edition). This book is easy to understand and learn c language errors.

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.