How can For each...
11735 단어 each
I understand the IEnumerator/IEnumerable methods and properties and also how they are interrelated. But I fail to understand from the
foreach loop perspective. Say for example, if I say the following line of code, how does the compiler look at
foreach loop and break it down to IEnumerable/IEnumerator for every occurance of
foreach loop in your code. In other words, what happens under the hood? I tried to go through couple of articles on the web. But they were NOT looked from the foreach loop perspective or I might have failed to understand. Could somebody explain with the example or direct me to any articles that explains clearly. All help is greatly appreciated.
Answer: To keep it short the compiler doesn't do much. It just translates the foreach code into a while under the hood. Here's an example:
List
........
foreach(int item in list)
{
//Do stuff
}
Becomes:
Enumerator
enum.Reset();
while(enum.MoveNext())
{
int item = enum.Current;
//Do stuff
}
or something very close to that. The acual code compiled most likely has a try/finally block around the while to dispose the enumerator
IEnumerable is the base interface for all non-generic collections that can be enumerated. For the generic version of this interface see System.Collections.Generic.IEnumerable<T> . IEnumerable contains a single method, GetEnumerator , which returns an IEnumerator . IEnumerator provides the ability to iterate through the collection by exposing a Current property and MoveNext and Reset methods.
It is a best practice to implement IEnumerable and IEnumerator on your collection classes to enable the foreach (For Each in Visual Basic) syntax, however implementing IEnumerable is not required. If your collection does not implement IEnumerable, you must still follow the iterator pattern to support this syntax by providing a GetEnumerator method that returns an interface, class or struct. When using Visual Basic, you must provide an IEnumerator implementation, which is returned by GetEnumerator. When developing with C# you must provide a class that contains a Current property, and MoveNext and Reset methods as described by IEnumerator , but the class does not have to implement IEnumerator .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CSharpExam
{
public class TestInumerable
{
public static void Run()
{
Person[] peopleArray = new Person[3]
{
new Person("Steven", "Xia"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
}
}
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class People //: IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
#region IEnumerable
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
#endregion
}
public class PeopleEnum //: IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【each 문 중첩 거동】 each 안에 each 안에 each왜 each 안에 each를 넣고 싶어졌는가 하면 이런 식으로 계층의 카테고리 기능을 작성하는 과정에서 필요하다고 생각했습니다. 내용이 이런 느낌으로 어려워지고 있습니다. 카테고리 수는 모두 400 가까이 ... 유...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.