471,319 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

IEnumerator and arraylist


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

Jan 29 '06 #1
3 4197
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/20...InnerClass.asp )

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"starter" <en*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

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

Jan 29 '06 #2
See the following resources:

http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...classtopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"starter" <en*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

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

Jan 29 '06 #3
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/...le.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;
}
}
}

Jan 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by juan | last post: by
3 posts views Thread by Hans | last post: by
1 post views Thread by midnight madness | last post: by
11 posts views Thread by Leslie Sanford | last post: by
5 posts views Thread by Shikari Shambu | last post: by
3 posts views Thread by EmilH | last post: by

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.