【发布时间】:2011-09-07 03:38:42
【问题描述】:
给定代码..
var dictionary = new Dictionary<string, string>{
{ "something", "something-else" },
{ "another", "another-something-else" }
};
dictionary.ForEach( item => {
bool isLast = // ... ?
// do something if this is the last item
});
我基本上想看看我在 ForEach 迭代中使用的项目是否是字典中的最后一个项目。我试过了
bool isLast = dictionary[ item.Key ].Equals( dictionary.Last() ) ? true : false;
但这没有用......
【问题讨论】:
-
您是否尝试过使用计数器并将通过 foreach 的迭代与字典的大小进行比较?字典理论上是无序的,所以我不相信任何与字典的 Last 值的比较。
-
字典中的“最后”到底是什么?
-
这个问题没有多大意义,因为
Dictionary集合没有排序。看看这个:Why is a Dictionary “not ordered”?
标签: c# linq dictionary