Principles of Foreach and javaforeach
Essence: implements an IEnumerable interface,
01. Why can I use foreach to traverse arrays and collections?
Resolution: Because arrays and collections both implement the IEnumerable interface, there is only one method in this interface, GetEnumerator ()
02. An array is a data structure that contains several variables of the same type. The array uses the type declaration: type [] arrayName;
03. The Array type is a reference type derived from the abstract base type Array. This type implements IEnumerable, so you can use foreach Iteration for all arrays in C. (From MSDN)
We all know that foreach can traverse the ArrayList set.
Let's take a look at the code written by Microsoft in F12.
01.
.
02.
03.
04.
Next we will simulate the implementation of Microsoft's methods:
1. The MyCollection class implements the IEnumerable interface.
Using System; using System. collections; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Foreach principle {// custom type: implements the IEnumerable interface to prove that the data stored in this type can be traversed by foreach public class MyCollection: IEnumerable {// 01 Add the public void Add (object o) {list to the set. add (o) ;}// 02 defines a set of ArrayList list = new ArrayList (); // 03 implement the GetEnumerator method public IEnumerator GetEnumerator () {return new MyIEnumerator (list );}}}
02. MyIEnumerator class implements the IEnumerator Interface
Using System; using System. collections; using System. collections. generic; using System. linq; using System. text; namespace Foreach principle {// IEnumerator: supports simple iteration of non-generic sets public class MyIEnumerator: IEnumerator {// defines a List set ArrayList list = new ArrayList (); public MyIEnumerator (ArrayList mylist) {// assign list = mylist to the set of the current class ;} // by default, the index of the set refers to private int I =-1 before the first data entry; // return the value of the Current loop traversal index public object Current {get {return list [I] ;}/// Movenext method public bool MoveNext () that implements the interface () {bool flag = false; // No data by default if (I <list. count-1) {// prove that the set has data to add 1 I ++ to the index; // change the bool value to true flag = true;} return flag ;} // initialize I to-1 public void Reset () {I =-1 ;}}}
03. Call the Main method
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Foreach principle {class Program {static void Main (string [] args) {// 01. instantiate the MyCollection class MyCollection list = new MyCollection () that implements the IEnumerable interface; // 02. add an element list to the set. add ("Xiao Zhang"); list. add ("Xiao Wang"); // 03. you can use the foreach loop to traverse the result foreach (string item in list) {Console. writeLine (item);} Console. readKey ();}}}
This is the principle or essence of the foreach traversal set or array.