Share via


Observable.Merge<TSource> Method (IObservable<TSource>[])

Merges all the observable sequences into a single observable sequence.

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

Syntax

'Declaration
Public Shared Function Merge(Of TSource) ( _
    ParamArray sources As IObservable(Of TSource)() _
) As IObservable(Of TSource)
'Usage
Dim sources As IObservable(Of TSource)()
Dim returnValue As IObservable(Of TSource)

returnValue = Observable.Merge(sources)
public static IObservable<TSource> Merge<TSource>(
    params IObservable<TSource>[] sources
)
public:
generic<typename TSource>
static IObservable<TSource>^ Merge(
    ... array<IObservable<TSource>^>^ sources
)
static member Merge : 
        sources:IObservable<'TSource>[] -> IObservable<'TSource> 
JScript does not support generic types and methods.

Type Parameters

  • TSource
    The type of source.

Parameters

Return Value

Type: System.IObservable<TSource>
The observable sequence that merges the elements of the observable sequences.

Remarks

The Merge operator is used to merge multiple observable sequences into a single observable sequence. Various overloads of this operator provide flexibility for specifying the sequences to be merged together. The merged sequence will continue to produce items in the merged sequence until all sequence have run to completion or one of the sequence produces an error.

Examples

The following example uses the Interval operator to create a sequence of integers starting with 0. A new integer is produced every 500ms. Two filtered sequence are created. One sequence filters the original sequence to produce every third integer. The other sequence filters the original sequence to produce only every 5th integer. The Merge operator is then used to merge these two filtered sequences into a single sequence of integers. A subscription is created for the merged sequence and each item is written to the console window.

using System;
using System.Reactive.Linq;
using System.Reactive.Concurrency;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** Generate a sequence of integers producing a new integer every .5 sec.                 ***//
      //*********************************************************************************************//

      var obsInt = Observable.Interval(TimeSpan.FromMilliseconds(500), Scheduler.ThreadPool);


      //*********************************************************************************************//
      //*** Filter the integer sequence to produce only every 3rd integer.                        ***//
      //*********************************************************************************************//

      var obsThrees = obsInt.Where(i => i % 3 == 0);


      //*********************************************************************************************//
      //*** Filter the integer sequence to produce only every 5th integer.                        ***//
      //*********************************************************************************************//

      var obsFives = obsInt.Where(i => i % 5 == 0);


      //***********************************************************************************************//
      //*** Subscribe to a merged sequence of the two filtered sequences. This merged sequence will ***//
      //*** produce every 3rd and every 5th integer.                                                ***//
      //***********************************************************************************************//

      var obs = Observable.Merge(new IObservable<long>[2] {obsThrees, obsFives});

      using (IDisposable handle = obs.Subscribe(x => Console.WriteLine(x)))
      {
        Console.WriteLine("Press ENTER to exit...\n");
        Console.ReadLine();
      }
    }
  }
}

The following output was generated by the example code.

Press ENTER to exit...

0
0
3
5
6
9
10
12
15
15
18
20
21
24
25
27
30
30

See Also

Reference

Observable Class

Merge Overload

System.Reactive.Linq Namespace