IEnumerator and arraylist

  • Thread starter Thread starter starter
  • Start date Start date
S

starter

I am trying to learn IEnumerator and collections(ArrayList, Hashtable)
in .NET.
Can anyone tell me what is IEnumerator used for and any online
resources would
be helpful.

Thanks
 
As you can see from the name of interface it is designated for enumaration
that is iteration of elements of container.

ArrayList in your situation can be considered container

to iterate over it following code can be used

ArrayList al = new ArrayList();
al.Add("str1");
al.Add("str2");
al.Add("str3");
foreach(string str in al)
{
Console.WriteLine(str);
}

If you have .NET 2.0 I'd recommed you use Generics to avoid boxing, when
using value types

When you implement IEnumerator in your container then foreach statement can
be used to enumerate the contents of container.

sample of IEnumerator implementation
(
http://www.c-sharpcorner.com/code/2002/Feb/IEnumeratorUsingInnerClass.asp )
 
Basic things first about interface.

Think of interface to mean 'feature list' or 'list of capabilities'.

When a class implements an interface, it actually means that class
will have those 'features' or 'capabilities' listed in that interface.
But this time, those features are not just listed but instead are
actually coded ( implemented in code).

If you get to understand this. You will see that

IEnumerator is an inteface that gives the ability to work with a
collection of objects. It has the 'list of features' so that you can
go through each object in the collection. The IEnumerator simply
specifies what are the things you need to do this. If you look at
the documentation is has these members

1. Current - pick current object
2. MoveNext - go to the next object
3. Reset - go back start all over again

Hey, think of it. Those simple three are just ENOUGH to get things
done. There is nothing more you need!!!!! That's serves exactly the
purpose of the interface.


Ok so much for the blah blah. Actual code now!

This article discusses and compares both versions in NET 1.X and .NET
2.0

http://www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c11193/


---- VERSION 1.X

class IteratorTest : IEnumerable
{
private ArrayList items = new ArrayList();

public int Count
{
get { return items.Count; }
}

public object this[int index]
{
get { return items[index]; }
set { items.Insert(index, value); }
}

public IEnumerator GetEnumerator()
{
return new IteratorSampleEnumerator(this);
}

private class IteratorSampleEnumerator : IEnumerator
{
IteratorTest aggregate = null;
int index = -1;

public IteratorSampleEnumerator(IteratorTest aggregate)
{
this.aggregate = aggregate;
}

public virtual object Current
{
get { return this.aggregate[index]; }
}

public virtual bool MoveNext()
{
index++;
if (index >= this.aggregate.Count)
{
return false;
}
else
{
return true;
}
}

public virtual void Reset()
{
index = -1;
}
}




--------- VERSION 2.0

class IteratorTest2<T> : IEnumerable
{
private ArrayList items = new ArrayList();

public int Count
{
get { return items.Count; }
}

public T this[int index]
{
get { return (T)items[index]; }
set { items.Insert(index, value); }
}

public IEnumerator GetEnumerator()
{
foreach (T item in this.items)
{
yield return item;
}
}
}
 
Back
Top