Let's say you wrote a custom enumerator for the code below:
public class School : IEnumerable<Student>
And then in the client code, you did this:
static void Main(string[] args)
{
var school = CreateSchoolWithStudents();
var query = from student in school
where student.Name.StartsWith("S")
select student;
Debugger.Break();
}
private static School CreateSchoolWithStudents()
{
return new School
{
new Student { Name = "Sathyaish" },
new Student { Name = "John" },
new Student { Name = "Carol" },
new Student { Name = "Peter" }
};
}
Then, set a break-point on the MoveNext
method implementation of your StudentEnumerator
class.
Then when you run the code and the debugger breaks after constructing the query / IEnumerable in this case, and you expand the Results View
like in the picture shown below, how does Visual Studio evaluate the sequence without breaking into its enumerator's MoveNext
?
I have always been curious about this.