Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface Collections: IEnumerable,IEnumerator

Status
Not open for further replies.

Stephen87

IS-IT--Management
Nov 18, 2006
4
US
Hi guys-
I know this is a broad question, but can someone help me understand IEnumerable and IEnumerator? I am in the dark on this one, and I have spent many many hours researching this, and I just dont understand the concept, nor do I understand how to make my own collections. Could someone help me out, and try to explain in theory, and in code? Thanks!
 
I'm not the expert on this subject but I have an example that may help.

In order to be able to iterate through a collection using foreach... your collection class needs to inherit from IEnumerable. This interface exposes a method (GetEnumerator) to allow you to iterate through the collection.

You create another class within your collection class that inherits from IEnumerator. This class is called by your GetEnumerator method in the collection class. You must implement the MoveNext, Reset and Current methods of IEnumerator. If you are familiar with ADO Recordsets, this functionality works very similarly-- using MoveNext to advance through the collection.

Code:
public class UserList:IEnumerable
	{
.
.
.
public IEnumerator GetEnumerator()
		{
			return new UserEnumerator(this.users);
		}

public class UserEnumerator : IEnumerator
		{
			#region IEnumerator Members

			private ArrayList users;
			private int index;

			public UserEnumerator(ArrayList users)
			{
				this.users = users;
				index = -1;
			}

			public object Current
			{
				get
				{
					if (this.index == -1)
						throw new InvalidOperationException(
							"You must call MoveNext before you call Current");
					if (this.index >= users.Count)
						throw new InvalidOperationException(
							"You have moved past the end of the collection.");
					return this.users[index];
				}
			}

			public bool MoveNext()
			{
				index++;
				return (index < users.Count);
			}

			public void Reset()
			{
				this.index = -1;
			}
            
			#endregion

		}
 
This allows you to enumerate the collection in your client code:

Code:
/* UserList is the collection, User represents your user class object where you might have LastName, FirstName, DateOfBirth properties */
foreach(User user in UserList)
{
    Console.WriteLine(user.LastName);
}
 
The conceptual difference is that IEnumerable says the collection can be enumerated.

How it gets enumerated is via the IEnumerator interface, which has methods to reset the enumerator, move to the next item, etc.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top