"Concat" is short for "concatenation". The Concat operator appends its input sequences one after another, returning one long, combined sequence.
The Concat operator already existed in LINQ, but the original LINQ operator could only concatenate two sequences at a time. The Rx operator is capable of concatenating any number of sequences simultaneously.
Concat is different than Merge, which asychronously combines its source sequences into one long sequence. If none of the source sequences throw, then the result of Concat and the result of Merge would have the same items, though possibly in a different order.
The Rx operator is nearly identical to the multi-parameter Concat operator in Jon Skeet's MoreLINQ library.
Concat may be invoked as a static method:
[TestMethod]
public void Concat_CalledAsStaticMethod_ConcatenatesSequences()
{
var test1 = new[] { 1 };
var test2 = new[] { 2, 3 };
var test3 = new[] { 4 };
var result = EnumerableEx.Concat(test1, test2, test3);
Assert.IsTrue(result.SequenceEqual(new[] { 1, 2, 3, 4 }));
}
Concat may also be invoked on a sequence of sequences, as an extension method:
[TestMethod]
public void Concat_CalledAsExtensionMethod_ConcatenatesSequences()
{
var test1 = new[] { 1 };
var test2 = new[] { 2, 3 };
var test3 = new[] { 4 };
var test = new[] { test1, test2, test3 };
var result = test.Concat();
Assert.IsTrue(result.SequenceEqual(new[] { 1, 2, 3, 4 }));
}
The result of Concat is an empty sequence if it is called with no arguments:
[TestMethod]
public void Concat_WithNoArguments_ReturnsEmptySequence()
{
var result = EnumerableEx.Concat<int>();
Assert.IsTrue(result.SequenceEqual(new int[] { }));
}
When called with only one sequence, Concat will return that sequence:
[TestMethod]
public void Concat_WithOneArgument_ReturnsSequence()
{
var test1 = new[] { 1 };
var result = EnumerableEx.Concat(test1);
Assert.IsTrue(result.SequenceEqual(new[] { 1 }));
}
Concat can take two arguments, just like the previouisly existing LINQ Concat.
[TestMethod]
public void Concat_WithTwoArguments_ConcatenatesSequences()
{
var test1 = new[] { 1 };
var test2 = new[] { 2, 3 };
var result = EnumerableEx.Concat(test1, test2);
Assert.IsTrue(result.SequenceEqual(new[] { 1, 2, 3 }));
}
Empty sequences work as expected:
[TestMethod]
public void Concat_WithEmptySource_ConcatenatesSequences()
{
var test1 = new[] { 1 };
var test2 = new int[] { };
var test3 = new[] { 4 };
var result = EnumerableEx.Concat(test1, test2, test3);
Assert.IsTrue(result.SequenceEqual(new[] { 1, 4 }));
}
Null sequences will raise a NullReferenceException when evaluated:
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void Concat_WithNullSource_ConcatenatesSequences()
{
var test1 = new[] { 1 };
var test3 = new[] { 4 };
var result = EnumerableEx.Concat(test1, null, test3);
// Concat raises an exception when the sequence is evaluated
result.Run();
}