.NET在迭代集合时如何决定项目的顺序?
例如:
我需要这个命令(访问集合成员):
例如:
list<string> myList = new List<string>();
myList.Add("a");
myList.Add("b");
myList.Add("c");
foreach (string item in myList)
{
// What's the order of items? a -> b -> c ?
}
我需要这个命令(访问集合成员):
for (int i = 1; i < myList.Count; i++)
{
string item = myList[i - 1]; // This is the order I need
}
我可以安全地使用foreach
与List
吗?其他类型的集合呢?