ArrayList.GetEnumerator(x,y) --- how to use?

  • Thread starter Thread starter Kevin P. Fleming
  • Start date Start date
K

Kevin P. Fleming

ArrayList implements an overload for IEnumerator's GetEnumerator method
as follows:

GetEnumerator(int offset, int count)

However, I can't see a way to take advantage of this, even though I'd
like to... I'd like to be able to use foreach to iterate over a section
of the ArrayList, but can't find any syntax that will work. The obvious:

foreach char ch in arList(0,20)
{
....
}

does not compile at all. Any ideas? Is this only usable if you actually
want the IEnumerator interface for something else besides foreach?
 
ArrayList implements an overload for IEnumerator's GetEnumerator method
as follows:

GetEnumerator(int offset, int count)

However, I can't see a way to take advantage of this, even though I'd
like to... I'd like to be able to use foreach to iterate over a section
of the ArrayList, but can't find any syntax that will work. The obvious:

foreach char ch in arList(0,20)
{
...
}

does not compile at all. Any ideas? Is this only usable if you actually
want the IEnumerator interface for something else besides foreach?

It would be great if C# would support such a syntax.
No You have to go the verbose way:

Enumerator e = array.GetEnumerator(0,20);
while(e.MoveNext())
{
Console.WriteLine(e.Current);
}

but for usage with ArrayList thats plain stupid and slow, better would be
here a normal for loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top