First say IEnumerable, we use the foreach every day do you really understand it?

Source: Internet
Author: User

Let's consider a few questions first:

    1. Why can't I modify the value of item in foreach?
    2. What are the conditions to implement foreach?
    3. Why do I want to return IEnumerable in LINQ to object?

Next, let's start with our text.

Implement iterators yourself

The. NET iterator is implemented through the IEnumerable and IEnumerator interfaces, and today we also come to the gourd.

First look at the definitions of these two interfaces:

It's not as complicated as it might seem. Where IEnumerable has only one GetEnumerator method that returns IEnumerator. In IEnumerator, there are two ways to add a property.

Following the development of the drawing scoop, we inherit the IEnumerable interface and implement:

The following is called using the original method:

A friend began to say, we are usually through the foreache to take value, not so used AH. OK, let's use the Foreach Loop:

Why is that basically equivalent? Let's look at the print results and look at the anti-compilation code.

Thus, there is a relationship between the two:

We can answer the first question, " Why can't I modify the value of item in foreach?" ":

Do we still remember the definition of IEnumerator?

The definition of an interface is only get without set. So we can't modify the value of item in foreach.

Let's answer the second question: "What are the conditions you need to implement a foreach?" ":

Must implement the IEnumerable interface? NO

We write our own myienumerable delete the back of the IEnumerable interface as can be foreach ( do not believe?) To test yourself ).

So to be able to foreach requires only that the object defines the GetEnumerator method, and that the return value is IEnumerator or its corresponding generic type. Look at

That is, as long as the three-step call can be satisfied. Do not necessarily inherit from IEnumerable. That's interesting! the next time the interviewer asks you to be sure to fight a struggle, haha!

Use of yield

You must have found out that we're going to have a little bit of trouble implementing the IEnumerator interface ourselves, and the code above is definitely not robust enough. Yes,. NET offers us a better way.

You'll find that we don't even have a myienumerator, and we can run normally. It's amazing. What does yield really do for us?

Boy, that big lump we wrote earlier. You're done with a yield keyword. The best thing about this piece of code:

This is the so-called State machine!

Let's continue to look at the definition and invocation of GetEnumerator:

When we call GetEnumerator, it seems that the inside for loop once, in fact, this time did not do any operation. The For loop is called only when MoveNext is called:

Now I would like to answer you "Why do I want to return IEnumerable in LINQ to Object?":

Because the IEnumerable is delayed loading , the value is only taken each time it is accessed. That is, the where, select that we write in Lambda does not loop through ( just in the assembly condition ), and only when the ToList or Foreache is actually set to the value. This greatly improves performance.

Such as:

This time got is the IEnumerable object, but did not go any traversal operation. ( against the above GIF animated view)

What, you still don't believe? Then let's do an experiment and implement mywhere ourselves:

Now you see it. There is no action when executing to Mywhere ( return is IEnumerable), only when the code is executed to ToList to actually traverse the filter.

Here the mywhere can actually be implemented by means of extension, to enhance the force lattice. (TheQuery Operators for LINQ are implemented in the form of extensions) [learn about extension methods].

how high-performance Random take the value in IEnumerable

This code source, "in-depth understanding of C #", personally feel very good. Paste it out for everyone to appreciate ha.

An individual's understanding and summary of iterators is not necessarily right. Pat on the TAP!

Article First chain: http://www.cnblogs.com/zhaopei/p/5769782.html

Demo Download: http://pan.baidu.com/s/1dE94c1b

Thank you for reading. if the article is useful to you, please give it a little encouragement.

First say IEnumerable, we use the foreach every day do you really understand it?

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.