Reputation: 28107
So IEnumerable
s don't guarantee order.
Does that mean if you do myEnumerable.Skip(5)
you cannot (unless you do .ToList()
or otherwise before) guarantee what will be returned?
Upvotes: 2
Views: 757
Reputation: 74277
Whether a particular ordering is guaranteed or not by any specdific IEnumerable<T>
is dependent on
An array will enumerate its contents in the obvious sequence (from x[0]
to x[n]
.) Ditto for a List<T>
, it being essentially an array of adjustable length. Actual [linked] lists, of course, can only be enumerated in order.
The order of enumeration of Dictionary<K,V>
, HashSet<T>
, binary trees, etc. is dependent upon the order in which objects were added. Add the same collection of values with differing orderings to a binary tree and the structure of the tree thus constructed will vary (the degenerate case, of course, being when objects are added in order, in which case the tree structure collapsed into an [ordered] linked list.
That being said, any particular instance of IEnumerable<T>
, should, barring any modifications to the underlying collection, yield the same sequence of values each time it is enumerated. That assumes, of course, a rational implementation of the interface. If the interface enumerates the collection by doing a random shuffle, of course, all bets are off.
If the actual order of items produced is important, you need to either
Upvotes: 3
Reputation: 203829
Once the objects are yielded by an IEnumerator
they do have an order. There is some item that comes out first, and some item that comes out second, etc. For some particular implementations that order might have meaning, for others it might be arbitrary, but there still is some order. The Skip
implementation is straightforward; it gets however many items without yielding them, and then gets the rest and yields them. Whether the items skipped mean anything in particular is the responsibility of whoever is calling the method.
Calling ToList
will never change the order of the items in the sequence, so adding such a call before calling Skip
wouldn't change anything. A call to OrderBy
on the other hand would result in a changed ordering, possibly from a meaningless order to a meaningful order. That's not to say it's required, merely that it can, in some situations, be a useful tool.
Upvotes: 5
Reputation: 48096
If you use Skip(x)
, the first x
elements will be ignored and everything after will be returned in a new IEnumerable<T>
. The interface makes no guarantees that it will retain order but in practice it does. Anytime you operate on an IEnumerable<T>
it will actually go through the same list in a linear fashion. For example if you read a file line by line into and IEnumerable<T>
the lines would always be in the same order as they were in the file (assuming you don't use a sorting method). Even if you use a Where
or some other method to filter the results, order will still be retained. The only thing you need to worry about is custom collections that implement IEnumerable<T>
. The collections in .NET will behave as you'd expect them to.
Upvotes: 0
Reputation: 415820
IEnumerable is an interface. As such, the interface has no guarantee of order. However, if you have an actual object that implements that interface, that object may (and often does) guarantee the order.
Upvotes: 0