How do I know if I'm iterating on the last item of the collection?

Posted by Camilo Martin on Stack Overflow See other posts from Stack Overflow or by Camilo Martin
Published on 2010-12-26T20:03:01Z Indexed on 2010/12/26 20:54 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

I want to do something different on the last KeyValuePair of the Dictionary I'm iterating on.

For Each item In collection
    If ItsTheLastItem
        DoX()
    Else
        DoY()
    End If
Next 

Is this possible?

Edit: As Jon correctly remarks, Dictionaries are not the kind of thing that's sorted, so I should mention that I only want to do this when displaying results to the user, and it doesn't matter if later on the last item is different.

In another note, I'll use my own answer but I'll accept the most upvoted one after I check back in some hours.

© Stack Overflow or respective owner

Related posts about c#

Related posts about vb.net