Hello. He began to search for collections and cycle foreach applicable to work with them. There is a banal, so to speak, training example: module element.cs
// Экземпляры этого класса будет содержать коллекция - UserCollection. public class Element { // Поля. private string name; private int field1; private int field2; // Конструктор. public Element(string s, int a, int b) { name = s; field1 = a; field2 = b; } // Свойства. public int Field1 { get { return field1; } set { field1 = value; } } public int Field2 { get { return field2; } set { field2 = value; } } public string Name { get { return name; } set { name = value; } } } UserCollections.cs module
// Класс UserCollection коллекция (набор) объектов класса Element. // Для применения foreach, необходимо, чтобы класс реализовывал интерфейс - IEnumerable. public class UserCollection : IEnumerable, IEnumerator { public Element[] elementsArray = null; public UserCollection() { elementsArray = new Element[4]; elementsArray[0] = new Element("A", 1, 10); elementsArray[1] = new Element("B", 2, 20); elementsArray[2] = new Element("C", 3, 30); elementsArray[3] = new Element("D", 4, 40); } // Указатель текущей позиции элемента в массиве. int position = -1; // ------------------------------------------------------------------------------------------------------------------ // Реализация интерфейса IEnumerator. // Передвинуть внутренний указатель (position) на одну позицию. public bool MoveNext() { if (position < elementsArray.Length - 1) { position++; return true; } else { return false; } } // Установить указатель (position) перед началом набора. public void Reset() { position = -1; } // Получить текущий элемент набора. public object Current { get { return elementsArray[position]; } } // ----------------------------------------------------------------------------------------------------------------- // Реализация интерфейса - IEnumerable. IEnumerator IEnumerable.GetEnumerator() { return this as IEnumerator; } } Module Program.cs
UserCollection myCollection = new UserCollection(); // Используем foreach, для обращения к каждому объекту Element внутри массива myCollection. foreach (Element element in myCollection) { Console.WriteLine("Name: {0} Field1: {1} Field2: {2}", element.Name, element.Field1, element.Field2); } //myCollection.Reset(); // Убрать комментарий для проверки. Console.Write(new string('-', 29) + "\n"); // Используем foreach, для повторного обращения к каждому объекту Element внутри массива myCollection. foreach (Element element in myCollection) { Console.WriteLine("Name: {0} Field1: {1} Field2: {2}", element.Name, element.Field1, element.Field2); } Console.Write(new string('-', 29) + "\n"); during the operation of which, the following is displayed: see figure 1 
If we remove the comment from the line (see Figure 2) 
myCollection.Reset(); which resets the position pointer to -1, i.e., ha, the predella of our collection, which allows another foreach cycle to be thrown out after that, again run through our collection and extracting its elements, and then during each iteration putting each element into the iteration variable — output to screen value of the extracted collection item. And when the above line was uncommented, after the first foreach cycle, the second one did not work - because the position pointer was already at the end of our collection.
After that, the author of the video course, which I look at, offers to review the work of the foreach loop along with the array and not with the collections.
int [] array={1,2,3,4,5,6,7,8,9,10} foreach(int titem in array) { Console.Writeline(item); } It then copies this foreach loop, and without inserting Reset () between them; - using two foreach cycles in a row without a gap in the form of Reset (); The most interesting thing that the author says, I quote - "In fact, all masses in C # are real collections. In C #, there are no arrays - this is just an illusion and a convenient syntax." Arguing this with this:
public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable that the Array class implements the IEnumerable interface - here on the forum I already asked about this interface. The contents of the IEnumerable interface. But here is my question: Why do we need to reset the position in the “ordinary” collections — for example, which we create ourselves — to re-run the collection using the foreach loop again, and in the usual array, which, by the author’s fame, is also a collection — we do not make this action? (although I didn’t go deep into the structure of the Array class and don’t know if there is any pointer in it at all? Maybe the Array class and implementing the IEnumerable interface in it (in the class) may not have such a mechanism - I don’t know)