-1

Consider the simple list of int's:

var ints = new List<int>
{
    1, 2, 3
};

Will it always be ordered in the same order (1, 2, 3) if I loop thru it? Even if I pass it as a argument to a function or if I pass it as a list for a thread to process?

Are Arrays different? What about IEnumerable?

What if I for instance:

ints.RemoveAt(1);
ints.Add(4);

Will it always be 1,3,4?

What about if I edited an element? These are only integers, but if they were complex objects, person, for instance and I grabbed person Bob (could be at any index) and for instance added his whole biography.

Would it change the order of the list?

I could use OrderBy, but I am merely interested to know if the order of a list is the same.

KJSR
  • 1,679
  • 6
  • 28
  • 51
Script0r
  • 117
  • 9
  • Yes. Any object is c# is always the same unless the value is changed. – jdweng Sep 26 '19 at 10:14
  • Did you read [remarks](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeat?view=netframework-4.8#remarks) when using msdn? For [Add](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add) method it's even the first sentence. – Sinatr Sep 26 '19 at 10:14
  • 3
    Did you check the docs? Arrays guarantee the order because they *are* arrays - a contiguous block of memory. Lists guarantee it too, and internally they use arrays. IEnumerable on the other hand is just an interface on top of *every* container. The order it returns items depends on that container – Panagiotis Kanavos Sep 26 '19 at 10:14
  • When you `Add` something in a list, it's added at a specified index. (the last one + 1). – Cid Sep 26 '19 at 10:14
  • Why this question? Is there a different problem behind it? Some operation that returned unexpected results perhaps? – Panagiotis Kanavos Sep 26 '19 at 10:15

2 Answers2

2

In the .Net framework Lists and arrays are index based collections.
This means that an element inside the collection can be accessed using it's index in the collection - so yes - they both have to retain the same order of elements inside.

This is because both List<T> and T[] implements the IList<T> interface, which is defined as a collection of objects that can be individually accessed by index.

An IEnumerable is not really sorted, all it does is expose a method called GetEnumerator() - and while the IEnumerator it returns does provide a Current property and a MoveNext() method which implies order - That's not really the case.

In fact, thanks to yield return you can create an IEnumearble even without a collection - for example:

Random rnd = new Random();
IEnumerable<int> someMethod()
{
     for(var i=0;i<10;i++)
         yield return rnd.Next(1, 10);
}

The IList<T> implements the IEnumerable<T> interface, but not the other way around.

So, to answer your questions:

Will it always be ordered in the same order (1, 2, 3) if I loop thru it? Even if I pass it as a argument to a function? Even if I pass it as a list for a thread to process?

Yes.

Are Arrays different?

Not in that respect.

What about IEnumerable?

They might be different.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

A list is basically just an advanced array with more functions.

This means that if you add something to a list it will stay in the same place

But it moves stuff an index down if you remove something in the middle of the list.

That means that if you have this list

List<int> numbers = new List<int>() {0, 1, 2, 3, 4};

And removes the item on index 1 you will end up with this list

{0, 2, 3, 4};

Which means that the number on index 2 now is on index 1.

The order of a list will only change if you change the list itself (Deletes and item, Add an item, or manually changes the order of the list (ex. orderBy)

It will not change if you change an object in the list.

The IEnumerable can be different because this is just an interface that specifies some methods that must be implemented like the GetEnumerator method. so that means that it depends on the implementation of the IEnumerable

(more info at this question "Can anyone explain IEnumerable and IEnumerator to me?"

Hope this helps