Observable.Concat(TSource) Method (IObservable(TSource), IObservable(TSource))

Concatenates two observable sequences.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

JScript does not support generic types and methods.

Type Parameters


The type of source.


Type: System.IObservable(TSource)
The first observable sequence.
Type: System.IObservable(TSource)
The second observable sequence.

Return Value

Type: System.IObservable(TSource)
An observable sequence that contains the elements of the first sequence, followed by those of the second the sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IObservable(TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.103).aspx or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.103).aspx.

The Concat operator runs the first sequence to completion. Then, the second sequence is run to completion effectively concatenating the second sequence to the end of the first sequence. Other overloads of this operator allow concatenating more than two observable sequences. When calling the Concat operator as an extension method, the first parameter is the sequence from which the extension method is being executed. This is demonstrated in the code example in this topic. The sequence passed to the operator as the second parameter will be concatenated to the first sequence only when the first sequence runs to completion. So it is important to note that the subscription to the second sequence is deferred until the first sequence runs to its completion. If it does not run to completion because of an exception or being blocked, then a subscription to the second sequence will not be created.

This example code demonstrates concatenating two sequences of integers to produce a sequence of the integers 1-6. Each integer is written to the console window.

using System;
using System.Collections.Generic;
using System.Reactive.Linq;

namespace Example

  class Program

    static void Main()
      int[] sequence1 = {1, 2, 3};
      int[] sequence2 = {4, 5, 6};
      //*** Create a new observable sequence of integers by concatenating sequence2 to sequence1 ***//
      IObservable<int> sequences = sequence1.ToObservable().Concat(sequence2.ToObservable());

      //*** The event handler for the subscription will just write each integer from the sequence to the console window. ***//
      sequences.Subscribe(i => Console.WriteLine(i));


The output from the example code is shown below.