Does Enumerable.Concat always append at the end of the first collection?
Example:
object someObject = new object();
List<object> listA = new List<object>();
listA.Add(new int());
object item = listA.Concat(new object[] { (object)new float() }).FirstOrDefault();
Is it guaranteed that the item
will be int
not float
after Concat
on every use? That means:
[0] int
[1] float
The MSDN says nothing about element order in resulting collection, however examples show that the order is elements from first collection then elements from second collection.
The Concat operator is defined for ordered multisets where the orders of the receiver and the argument are the same. Ordering in SQL is the final step before results are produced. For this reason, the Concat operator is implemented by using UNION ALL and does not preserve the order of its arguments.
You cannot, because IEnumerable<T> does not necessarily represent a collection to which items can be added. In fact, it does not necessarily represent a collection at all! For example: IEnumerable<string> ReadLines() { string s; do { s = Console.
Concat
is a LINQ method. That means it's a query. It does not create a list or some other kind of collection but a sequence.
So what Concat
actually does is combining the two source sequences. When you iterate through the result of Concat
you first iterate through the first sequence and then through the second sequence. The sequence are thereby never changed.
So, yes
the item will be
int
notfloat
after Concat on every use
The MSDN says nothing about element order
Well it does say
Concatenates two sequences.
And to concatenate
means to put one after the other, not to mix them up.
From the reference source:
public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) {
if (first == null) throw Error.ArgumentNull("first");
if (second == null) throw Error.ArgumentNull("second");
return ConcatIterator<TSource>(first, second);
}
static IEnumerable<TSource> ConcatIterator<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second) {
foreach (TSource element in first) yield return element;
foreach (TSource element in second) yield return element;
}
So you see the two consecutive foreach
will first yield the elements of the first sequence, then the element of the second sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With