18

Assume two lists, A and B so that A = (1,2,3) and B = (4,5,6). Will A.Concat(B) preserve the order so that the result is (1,2,3,4,5,6)?

sduplooy
  • 14,340
  • 9
  • 41
  • 60
  • 2
    Why the H*LL downvote this? Upvoting to counter unless someone can provide a good reason. If you need clarrification then ask for it in the comments... – Rory Becker Feb 03 '09 at 15:22
  • possible duplicate of [Preserving order with LINQ](http://stackoverflow.com/questions/204505/preserving-order-with-linq) – Amy B Dec 04 '10 at 22:07

2 Answers2

16

Yes. IEnumerable.Concat will simply turn two list into a single list by attaching one to the end of the other. Order within each list will be preserved.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    Is it explicitly mentioned somewhere in documentation? I mean whether it is a subject to change as an implementation detail, or is it clearly stated that this behaviour won't change in future. – Dmitrii Lobanov Apr 21 '11 at 06:40
  • 1
    @Dmitry MS can't change this behavior without breaking a huge number of applications and redefining the meaning of concatenate. IMHO, it's safe to depend on this. – JaredPar Apr 21 '11 at 16:16
  • Agree that this should have been mentioned in the documentation – variable Feb 09 '22 at 17:22
8

Yes, that's pretty much what concatenation means.

Obligatory MSDN quote: (Enumerable.Concat)

Return Value

Type: System.Collections.Generic.IEnumerable(TSource)

An IEnumerable(T) that contains the concatenated elements of the two input sequences.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176