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

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -