"Spare Change" <sp*********@spam.spam> wrote in
news:xP***************@newsread3.news.pas.earthlin k.net...
Say I am iterating through an objects values:
int i;
DirectoryInfo di = new DirectoryInfo("c:/inetpub/wwwroot/demos");
FileInfo[] rgFiles = di.GetFiles("*.aspx");
foreach(FileInfo fi in rgFiles)
htmlPgs[i++]=fi.Name;
What property of rgFiles could be substituted for i?
That is, can I write:
htmlPgs[rgFiles.<enumerator>];
instead?
No: that's the main disadvantage of a foreach loop;
Internally the foreach loop queries the collection object for the
IEnumerable interface, gets an IEnumerator object from it, which only
exposes the methods "Current", "MoveNext", and "Reset": There's no way to
get an index from it, unless the object in question has some special
provisions for that (I know none that would).
I guess the main reason for this architecture is to ensure the foreach loop
can be used on any kind of collection: If you're iterating over a linked
list, database query keeping an index would be quite complex, as the
collection may change while it's iterated.
Niki