Export (0) Print
Expand All

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)

public static IObservable<TSource> Merge<TSource>(
	params IObservable<TSource>[] sources
)

Type Parameters

TSource

The type of source.

Parameters

sources
Type: System.IObservable<TSource>[]
The observable sequences.

Return Value

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

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.

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
Show:
© 2014 Microsoft