IEnumerable first. Do you really understand the foreach we use every day ?, Ienumerableforeach

Source: Internet
Author: User

IEnumerable first. Do you really understand the foreach we use every day ?, Ienumerableforeach

Let's first think about several questions:

Next, let's start our text.

Self-implemented iterator

In. net, the iterator is implemented through the IEnumerable and IEnumerator interfaces. Today, we will also draw images based on Huludao.

First, let's take a look at the definitions of these two interfaces:

It is not as complicated as you think. IEnumerable only has one GetEnumerator method that returns IEnumerator. In IEnumerator, two methods add an attribute.

Next we will develop the image scheme. We inherit the IEnumerable interface and implement it:

The following uses the original method for calling:

Some friends started to say that we usually use foreache for values. We have never used it like this. Okay, let's use the foreach loop:

Why is it basically equivalent? Let's take a look at the printed results and look at the decompilation code.

It can be seen that the two have such a relationship:

We can answer the first question: "Why cannot I modify the item value in foreach ?" :

Do we still remember the definition of IEnumerator?

 

Only get does not have set. Therefore, the item value cannot be modified in foreach.

Let's answer the second question: "What conditions must foreach be met ?" :

Must I implement the IEnumerable interface?NO

In MyIEnumerable we write, the IEnumerable interface after deleting is the same as that of foreach (do not believe it? Test by yourself ).

Therefore, foreach only requires that the object defines the GetEnumerator method without parameters, and the returned value is IEnumerator or its corresponding generic type. Take a closer look:

That is to say, you only need to meet the three-step call. It does not have to inherit from IEnumerable. Interesting!The next time the interviewer asks you, you must fight for death. Haha!

Yield usage

You must have discovered that implementing the IEnumerator interface by yourself is still a little troublesome, and the above Code is definitely not robust enough. Yes,. net provides us with a better way.

You will find that we don't even need MyIEnumerator, and it can also run normally. It's amazing. What did yield do for us?

Good guy, the one we wrote before. You can get a yield keyword. The best thing is this Code:

This is the so-called state machine!

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

When we call GetEnumerator, it seems that there is a for loop in it. In fact, there is no operation at this time. The for loop is called only when MoveNext is called:

Why is IEnumerable returned in Linq to Object ?" :

Because IEnumerable isDelayed Loading. That is, weThe where and select statements written in Lambda do not have circular traversal (only in the assembly conditions)The set value can be used only when ToList or foreache is used. This greatly improves the performance.

For example:

In this case, the IEnumerable object is obtained, but no traversal is performed. (Compare to the gif animation above)

What, do you still believe it? Let's create an experiment to implement MyWhere:

The returned result is IEnumerable.), The code can be traversed and filtered only when the ToList is executed.

Here, MyWhere can actually be implemented by using the extension method to increase the limit. (The query operators of Linq are implemented in the form of extensions.) [understand the extension method].

How high performance Random Obtain the value in IEnumerable.

I personally think this code is from a deep understanding of C. Post it to everyone.

 

My personal understanding and summary of the iterator are not necessarily true. Pat!

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

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

Thank you for reading this article. If the article is useful to you, please give me a thumbs up to encourage you.

 

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.