C# – Ordered Enumeration: IEnumerable or Array?

arraycorder

Typical context: I make an extension method for a collection which considers that the elements are ordered. The function starts at the beginning, at index 0, and the order has significance. Examples: Grouping by sequence or Indexes of an item.

However, I'm always stumped as to what to extend: IEnumerable<T> or T[]. My rationale was clarity of purpose: An array has a notion of ordering, whereas an IEnumerable is implemented by many generic collections, not all of which have a notion of order:

  • Dictionary – unordered
  • HashSet – unordered
  • LinkedList – ordered
  • List – ordered
  • Queue – ordered
  • SortedDictionary – sorted (not original order)
  • SortedList – sorted (not original order)
  • SortedSet – sorted (not original order)
  • Stack – reversed

As well as any other implementation which might or might not be ordered.

Also, I'm not certain if the enumerator is reset if an enumeration is not completed, so if that's a worry, then who knows at what point the enumeration would start? Enumerating an array would always start at the beginning.

So to me, it makes more sense to extend a T[]. But am I correct in thinking that? Am I worrying too much? What's the proper approach for ensuring "ordered" enumeration?

Best Answer

I'm not certain if the enumerator is reset if an enumeration is not completed, so if that's a worry, then who knows at what point the enumeration would start? Enumerating an array would always start at the beginning.

These two sentences make me think that you have deep misunderstandings about how the enumerable pattern works. Can you explain why you think that an abandoned enumerator has anything whatsoever to do with a later enumeration?

An enumerable is a device which produces enumerators. If an enumerator is abandoned, that does not in any way affect the enumerable. If I sell books, and Bob buys a book and only reads it halfway, that doesn't mean that when I sell a different copy of the book to Alice, she has to start reading where Bob left off.

I'm always stumped as to what to extend: IEnumerable<T> or T[]. My rationale was clarity of purpose: An array has a notion of ordering, whereas an IEnumerable is implemented by many generic collections, not all of which have a notion of order:

Does your extension method need to (1) access the collection out of order? (2) write to the collection?

If so, then extend IList<T> If not, extend IEnumerable<T>.

What about: (3) pass the collection on to a method that expects an array?

Then extend array.

Your question is basically "I don't know whether to extend Animal or Giraffe". If your extension method is not specific to Giraffes, extend Animal. If your extension method is not specific to arrays, extend all lists. If it is not specific to lists, extend all sequences. If it is not specific to sequences, odds are good that an extension method is the wrong mechanism for you; I recommend against extending all objects.

Related Topic