C# foreach iterating rule for collections -
how .net decides order of items when iterating through collections ?
for example:
list<string> mylist = new list<string>(); mylist.add("a"); mylist.add("b"); mylist.add("c"); foreach (string item in mylist) { // what's order of items? -> b -> c ? } i need order (accessing collection members):
for (int = 1; < mylist.count; i++) { string item = mylist[i - 1]; // order need } can safely use foreach list? other types of collections?
.net doesn't decide - class implementing ienumerable decides how being done. list index 0 last one. dictionary depends on hash value of key think.
list indexes 0 based, can this:
for (int = 0; < mylist.count; i++) { string item = mylist[i]; // order need } and same result foreach. if want explicit stick loop. nothing wrong that.
Comments
Post a Comment