how do I check if an entity is the first element of a foreach loop

Viewed 94724

Say I have a foreach loop.

I have to do something with the first object of the loop that I don't have to do with any of the other objects.

How do I check if the item that's currently in the loop is the first object.

9 Answers

In my opinion this is the simplest way

foreach (var item in list)
{
    if((list.IndexOf(item) == 0)
    {
        // first
    }
    // others
}
Related