【发布时间】:2011-05-22 08:56:31
【问题描述】:
通常我必须编写一个需要为第一项特殊情况编写的循环,代码似乎永远不会像理想情况下那样清晰。
没有重新设计 C# 语言,编写这些循环的最佳方法是什么?
// this is more code to read then I would like for such a common concept
// and it is to easy to forget to update "firstItem"
foreach (x in yyy)
{
if (firstItem)
{
firstItem = false;
// other code when first item
}
// normal processing code
}
// this code is even harder to understand
if (yyy.Length > 0)
{
//Process first item;
for (int i = 1; i < yyy.Length; i++)
{
// process the other items.
}
}
【问题讨论】:
-
我真的看不出(你的第一个例子)检查布尔值有什么问题,任何看到它的人都会很快知道你在做什么。
标签: c# collections