C # foreach,

Source: Internet
Author: User

C # foreach,

Foreach can traverse some data types. The figure shows that the data type to be traversed must have the public method of GetEnumerator.

When the foreach time involves the var inference type, the object type is used instead of the generic version.

Foreach traversal can only read data, but cannot modify data. It can be checked by string. string can be traversed, but its char cannot be changed. (Only in the following scenarios ).

The foreach traversal only goes in, and reads one by one, which is similar to the Read method of the DataReader that operates the database.

 

The above analysis shows that the IEnumerator method must be defined inside the class to be traversed. There are two solutions here.

Method 1: use yield

Yield return expression. If the returned value is IEnumerable <T>, an iteration type is automatically generated.

If the returned value is IEnumerator <T>, an iterator is automatically generated. The IEnumerator type is an iterator.

You can obtain the value of the person array during traversal.

Method 2: implement the IEnumerable Interface

IEunmerable includes the GetEunmetator method, and the returned value is of the IEnumerator type.

In this case, another class must inherit the IEnumerator interface to return its subclass.

Implementation process:

1. You need to add int-type variables to mark the index location. Int position =-1 is used here.

2. Add the corresponding array variable to receive the passed array. Here we use string [] persion;

3. For the implemented Curren attribute, the data of the current index needs to be returned. Return person [position];

4. To implement the MoveNext method, add the pointer (INDEX) to 1 and move forward. The return value is of the bool type to determine whether the index is operated.

5. Implement the Reset method. This is the Reset method. Assign the position value to-1;

6. Add a constructor with parameters to receive the passed data.

Iii. List Set Analysis

The reflection tool shows that the Add method of the List set is to Add a value to the internal item, and this item is an array. It is displayed as T because it is generic.

That is, the set contains such a variable to store the added data, just like the array defined above. In this way, the List set class is similar to the preceding

The custom classes are the same. The above definition is only to demonstrate the process. Of course, you can expand the above classes by yourself out of business needs, such

Changing to the Add method and filtering data logic can be expanded according to business needs.

 

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.