Часто мне приходится закодировать цикл, который требует особого случая для первого элемента, код никогда не кажется таким ясным, каким должен быть в идеале. .
Если не считать редизайна языка 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.
}
}