C#foreach集合的迭代规则
|
.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
一起使用吗?那其他类型的收藏呢?
没有找到相关结果
已邀请:
5 个回复
掀辟髓观粟
的类决定了它是如何完成的。对于
,它从索引0到最后一个索引。对于
,它取决于我认为的键的哈希值。
索引基于0,因此您可以执行以下操作:
与
的结果相同。但是,如果您想对其进行明确说明,则只需坚持使用for循环即可。没有错。
锹缄
阀傻
诞胃
誓猎贰