foreach Instance code in C #

Source: Internet
Author: User
foreach can traverse some data types. The graph shows that the data type being traversed must have a public method of GetEnumerator.

The use of the foreach diachronic involves the var inference type, when the generic version is not implemented, then it is the object type.

The foreach traversal can read only the data, cannot modify the data, can be detected by string, the string can be traversed, but the char that makes up it cannot be changed. (only in sub-scenarios).

The foreach traversal is a forward-only, one-by-one reading, similar to the DataReader read method of manipulating the database.

The above analysis shows that the IEnumerator method needs to be defined within the class to be traversed. There are two ways to solve this.

Method one, through yield

Yield return expression, or, if the return value is ienumerable<t>, an iteration type that is automatically generated.

A return value of ienumerator<t> indicates that an iterator is automatically generated. Where the IEnumerator type is an iterator.

The value of the in-person array can be obtained with this traversal.

Method two by implementing the IEnumerable interface

Ieunmerable includes the Geteunmetator method, and the return value is the type of IEnumerator.

At this point, you need another class to inherit the IEnumerator interface, so that you can return its subclasses to achieve your goal.

Implementation process:

1. You need to add a variable of type int to mark the position of the index. here with int position=-1.

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

3. The Curren property that is implemented requires that the data for the current index be returned. return person[position];

4. Implement the MoveNext method, you need to add the pointer (index) 1, move forward, the return value is the bool type, used to determine whether the index is manipulated.

5. Implement the Reset method, which is the reset method, assigning the position to-1;

6. Add a constructor with parameters that receives the data passed in.

Three, List set analysis

The reflection tool shows that the Add method of the list collection is to add a value to the internal item, and this item is an array. Because it is generic, it is displayed as T.

That is, the inside of the collection has a variable that stores the added data, just like the above-defined array, so that the list collection class is

The custom class is consistent, the definition above is just to demonstrate the process, of course, for business needs, it is possible to implement the extension of the above class, for example, the static data

Change to add method, filter data logic, etc., can be expanded according to business requirements.

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.