none
Generic List order RRS feed

  • Question

  • Hi,

    looking at the generic List documentation it appears that the order of the items will not be guaranteed.

    Empirically it always stays the same, but I appreciate this behavior might change in the future as it's not in its specifications.

    If I need a simple generic collection allowing duplicates and insertion/removal which guarantees its order, shall I use the generic LinkedList or is there any other class matching my requirements?

    Thanks.

    Sunday, May 31, 2015 10:16 AM

Answers

  • "As it is logic what we're talking about, not potatoes, the "obvious" should just be what can be a natural unique logical consequence of another statement."

    Yes but the .NET documentation is rather informal and using pure logic to approach it won't work too well, a bit of common sense is required.

    "makes me think there is an intention behind, to make sure they will be allowed to change this in the future without contradicting themselves in the future."

    Nope, it's just an oversight in the documentation. There's absolutely no way that the iteration order of List<T> will ever change, that will break pretty much every .NET application that was ever written.

    Thursday, June 4, 2015 8:50 PM
    Moderator

All replies

  • "looking at the generic List documentation it appears that the order of the items will not be guaranteed."

    What makes you think so? You're supposed to be able to access the list elements by index, if the list would randomly change the order of element such indexed access would be completely useless.

    • Proposed as answer by Blackwood Sunday, May 31, 2015 1:53 PM
    Sunday, May 31, 2015 10:32 AM
    Moderator
  • That was my initial thought; not to be too captious, but the index does not imply that I will get the right order when I use the iterator... I know it could be assumed as a natural consequence, but you could have: list[0] = 0, list[1] = 1, but the iterator could start from index 1. Really absurd, but how can I know it will never happen? why not saying it clearly in the documentation?

    Sunday, May 31, 2015 3:05 PM
  • "but the iterator could start from index 1"

    Well, it can't because the documentation for List<T>.GetEnumerator() says:

    "Initially, the enumerator is positioned before the first element in the collection"

    so it wouldn't make sense for enumeration to start from index 1.

    "why not saying it clearly in the documentation?"

    Probably because nobody thought that such an elementary characteristic of List<T> needs to be explicitly stated in the documentation.

    Sunday, May 31, 2015 5:31 PM
    Moderator
  • "Initially, the enumerator is positioned before the first element in the collection"

    Ok, let's assume the first element is 0, it doesn't state that the next one is 1, it could be 2.

    "Probably because nobody thought that such an elementary characteristic of List<T> needs to be explicitly stated in the documentation."

    As it is logic what we're talking about, not potatoes, the "obvious" should just be what can be a natural unique logical consequence of another statement.

    Since the collection is not called ImmutableOrderList, I believe the order of the iteration is not mandatory and the fact that it's omitted, makes me think there is an intention behind, to make sure they will be allowed to change this in the future without contradicting themselves in the future.

    The fact that empirically the order is always right, is not enough if you are building a missile control system and you want to make sure it will never go back and destroy the launch site, I believe a defence software should be based on exact specification.

    If there is no guarantee and no other collection meeting this requirement, it might be worth creating a custom collection.

    Thursday, June 4, 2015 5:58 PM
  • "As it is logic what we're talking about, not potatoes, the "obvious" should just be what can be a natural unique logical consequence of another statement."

    Yes but the .NET documentation is rather informal and using pure logic to approach it won't work too well, a bit of common sense is required.

    "makes me think there is an intention behind, to make sure they will be allowed to change this in the future without contradicting themselves in the future."

    Nope, it's just an oversight in the documentation. There's absolutely no way that the iteration order of List<T> will ever change, that will break pretty much every .NET application that was ever written.

    Thursday, June 4, 2015 8:50 PM
    Moderator