none
When I use foreach on List<T>, is the order guaranteed?

    Question

  • Another newbie question: If I use foreach on a generic List (List<T>), is the enumeration order guaranteed? I mean, Will I get the same order of elements as I added them into the List<T>?

    Thank you in advance.
    Wednesday, January 02, 2008 3:20 PM

Answers

  • Yes. An (IList) is ordered, and iterating will iterate from the first item to the last, in order inserted (assuming you always inserted at the end of the list as opposed to somewhere else, depending on the methods you used). Unless you're talking about some special class named "List" as you posted, rather than about the standard IList interface and implementations of it.

     

    Wednesday, January 02, 2008 3:24 PM

All replies

  • Yes. An (IList) is ordered, and iterating will iterate from the first item to the last, in order inserted (assuming you always inserted at the end of the list as opposed to somewhere else, depending on the methods you used). Unless you're talking about some special class named "List" as you posted, rather than about the standard IList interface and implementations of it.

     

    Wednesday, January 02, 2008 3:24 PM
  • I got an additional question, do you get it from practice or it is specified by .NET framework?
    Wednesday, January 02, 2008 3:28 PM
  • Implementations of IList and IList<T> should always return enumerators that traverse the list's items in the order they exist within the list.  I'm not 100% sure if this is explicitly specified in any standard are official documentation, but I wouldn't be surprised since this is how all the .NET Framework implementations of IList and IList<T> behave.

    Wednesday, January 02, 2008 3:36 PM
  • The documentation of IList doesn't spell it out regarding the order. But it can be inferred by the fact that there are Insert and Item methods which accept a numeric index -- if you Insert an item at index 5 (for example, at the end of the list), and can get that same item back using an index of [5], then obviously the order must be guaranteed.

     

    Wednesday, January 02, 2008 3:48 PM
  • Thanks for the answers, I still have a doubt on this issue because I saw this:

    The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

    http://technet.microsoft.com/en-us/library/ttw7t8t6(VS.80).aspx

    I am wondering what kind of modification will actually change the result returned from foreach ...
    Wednesday, January 02, 2008 4:12 PM
  • I am wondering what kind of modification will actually change the result returned from foreach ...

    Inserting or deleting items to/from the collection being iterated over is what that is mainly talking about. But since the article is just about the "foreach" statement in general (not specifically for using it on an IList), in general you shouldn't change the underlying collection, such as changing a key in a Dictionary -- that would change the order of the collection for example.

    Wednesday, January 02, 2008 4:42 PM