Export (0) Print
Expand All

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

Concatenates two observable sequences.

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

public static IObservable<TSource> Concat<TSource>(
	this IObservable<TSource> first,
	IObservable<TSource> second
)

Type Parameters

TSource

The type of source.

Parameters

first
Type: System.IObservable<TSource>
The first observable sequence.
second
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 http://msdn.microsoft.com/en-us/library/bb384936.aspx or http://msdn.microsoft.com/en-us/library/bb383977.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));

      Console.ReadLine();     
    }
  }
}

The output from the example code is shown below.

1
2
3
4
5
6
Show:
© 2014 Microsoft