473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about IEnumerable and Interface Signatures

In the code below from MSDN

How do the PeopleEnum methods ever get called?

foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

What is going on behind the scenes in the foreach?

Also, I could not find where the interface signatures were for the
IEnumerable and IEnumerator. How did they know what methods they were
required to use from the interface signature. I looked them up on
MSDN, but didn't see any references to GetEnumerator or MoveNext.
Just this type explanation:

[ComVisibleAttribute(true)]
[GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator

http://msdn.microsoft.com/en-us/libr...le(VS.80).aspx

Thanks.

---
using System;
using System.Collections;

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];
}
}

public IEnumerator GetEnumerator()
{
return new PeopleEnum(_people);
}
}

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();
}
}
}
}

class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};

People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

}
}

/* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/
Jun 27 '08 #1
4 1722
On Jun 12, 5:01 pm, jmDesktop <needin4mat...@gmail.comwrote:
In the code below from MSDN

How do the PeopleEnum methods ever get called?

foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

What is going on behind the scenes in the foreach?
foreach calls GetEnumerator() on the IEnumerable to get an
IEnumerator, then calls MoveNext() and Current on the IEnumerator,
executing the body of the loop each time, until MoveNext() returns
false or the body of the foreach loop exits early somehow (break/
return/exception).

It then (irrespective of whether it reached the end or not) checks
whether or not the IEnumerator implements IDisposable, and calls
Dispose if so. (The generic IEnumerator<Tinterface extends
IDisposable, so if there's a generic IEnumerable<Tthe compiler
doesn't need to perform an execution-time check here - it just calls
Dispose.)
Also, I could not find where the interface signatures were for the
IEnumerable and IEnumerator. How did they know what methods they were
required to use from the interface signature. I looked them up on
MSDN, but didn't see any references to GetEnumerator or MoveNext.
The interfaces IEnumerable and IEnumerator are both defined in the
System.Collections namespace, and the methods within them are
documented in MSDN in the normal way. The generic forms are in the
System.Collections.Generic namespace.

In fact, the C# compiler can use foreach without implementing
IEnumerable and IEnumerator at all - but it's extremely rare.

Jon
Jun 27 '08 #2
Rather that explaining here what happens, I would suggest you compile the
code and open the assembly in ildasm.exe to exactly see what the compiler
does behind the scenes. Thats the best way IMO to know.

Sujeet

"jmDesktop" wrote:
In the code below from MSDN

How do the PeopleEnum methods ever get called?

foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

What is going on behind the scenes in the foreach?

Also, I could not find where the interface signatures were for the
IEnumerable and IEnumerator. How did they know what methods they were
required to use from the interface signature. I looked them up on
MSDN, but didn't see any references to GetEnumerator or MoveNext.
Just this type explanation:

[ComVisibleAttribute(true)]
[GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator

http://msdn.microsoft.com/en-us/libr...le(VS.80).aspx

Thanks.

---
using System;
using System.Collections;

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];
}
}

public IEnumerator GetEnumerator()
{
return new PeopleEnum(_people);
}
}

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();
}
}
}
}

class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};

People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

}
}

/* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/
Jun 27 '08 #3
Adding to what John said, specifically for foreach statement the 'peopleList'
type does not have to implement IEnumearble. It can also just have a method
GetEnumerator() that returns a type Person that has a public instance
'MoveNext()' method that returns a bool and a public instance property
'Current' that returns the type 'Person'

But IMO implementing IEnumerable explicitly is more cleaner and easy to read.

Sujeet

"Jon Skeet [C# MVP]" wrote:
On Jun 12, 5:01 pm, jmDesktop <needin4mat...@gmail.comwrote:
In the code below from MSDN

How do the PeopleEnum methods ever get called?

foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

What is going on behind the scenes in the foreach?

foreach calls GetEnumerator() on the IEnumerable to get an
IEnumerator, then calls MoveNext() and Current on the IEnumerator,
executing the body of the loop each time, until MoveNext() returns
false or the body of the foreach loop exits early somehow (break/
return/exception).

It then (irrespective of whether it reached the end or not) checks
whether or not the IEnumerator implements IDisposable, and calls
Dispose if so. (The generic IEnumerator<Tinterface extends
IDisposable, so if there's a generic IEnumerable<Tthe compiler
doesn't need to perform an execution-time check here - it just calls
Dispose.)
Also, I could not find where the interface signatures were for the
IEnumerable and IEnumerator. How did they know what methods they were
required to use from the interface signature. I looked them up on
MSDN, but didn't see any references to GetEnumerator or MoveNext.

The interfaces IEnumerable and IEnumerator are both defined in the
System.Collections namespace, and the methods within them are
documented in MSDN in the normal way. The generic forms are in the
System.Collections.Generic namespace.

In fact, the C# compiler can use foreach without implementing
IEnumerable and IEnumerator at all - but it's extremely rare.

Jon
Jun 27 '08 #4
Sujeet <Su****@discussions.microsoft.comwrote:
Adding to what John said, specifically for foreach statement the 'peopleList'
type does not have to implement IEnumearble. It can also just have a method
GetEnumerator() that returns a type Person that has a public instance
'MoveNext()' method that returns a bool and a public instance property
'Current' that returns the type 'Person'

But IMO implementing IEnumerable explicitly is more cleaner and easy to read.
Agreed - although I'd implement IEnumerable<Personand do it with an
iterator block :)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Rundle | last post by:
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
2
by: Peter Rilling | last post by:
A design pattern question. As you know, an enumerator in .NET is broken into two interface (IEnumerable and IEnumerator). Is there a benefit in having two interfaces? Why not just have the...
10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
3
by: KH | last post by:
I can't seem to figure this one out... I've searched MSDN and Goog, and made my best guesses to no avail,, so help would be much appreciated! public ref class T sealed : public...
2
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning...
1
by: Larry | last post by:
I checked definition of class CollectionBase public abstract class CollectionBase : IList, ICollection, IEnumerable, it implements 3 interface IList, ICollection and IEnumerable. I found...
1
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
Something I found surprising this week involves the IEnumerable<Tinterface. I have a class that I wrote a couple of years ago which implements the IEnumerable interface. This week I realized it...
20
by: -- | last post by:
Imagine I have a class TypeX and a class TypeY that inherts TypeX. public class typeX { .... } public class typeY : typeX { ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.