经常情况下我需要编写循环,为第一个项目提供特殊情况的处理,但代码似乎从来都不像理想情况下那么清晰。
除了重新设计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.
}
}