473,408 Members | 1,938 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,408 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 4301
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid...
1
by: juan | last post by:
hi i have a teachers class where i want the user to enter a few teachers and iterate thru them. In the main i get an error System.Collections.IEnumerator' does not contain a definition for...
3
by: Hans | last post by:
I implemented the IEnumerator interface in some classes to enable the use of the foreach statement. As you probalbly know, the interface asks for the implementation of object IEnumerator.Curren...
1
by: Matthias S. | last post by:
Hi, I'm trying to provide the user of my class access to the objects in a collection without allowing him to modify (add/remove/clear items) the collection. I thought I can do it by providing a...
1
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the...
11
by: Leslie Sanford | last post by:
I've been kicking around an idea mostly as a thought experiment at this point. The idea is to create a thread safe collection that implements the IEnumerable interface. The enumerators it creates...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
3
by: EmilH | last post by:
Hi. I have read some examples of IEnumerator at http://www.codeproject.com/csharp/csenumerators.asp What if I have 2 arrays or collections, for example a DataSet which has 2 DataTables in my...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.