Observable.Merge<TSource> メソッド (IObservable<TSource>[])
すべての監視可能なシーケンスを 1 つの監視可能なシーケンスにマージします。
Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)
構文
'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.
型パラメーター
- TSource
ソースの種類。
パラメーター
- sources
型: System.IObservable<TSource>[]
監視可能なシーケンス。
戻り値
種類: System.IObservable<TSource>
監視可能なシーケンスの要素をマージする監視可能なシーケンス。
解説
Merge 演算子は、複数の監視可能なシーケンスを 1 つの監視可能なシーケンスにマージするために使用されます。 この演算子のさまざまなオーバーロードにより、結合するシーケンスを柔軟に指定できます。 マージされたシーケンスは、すべてのシーケンスが完了するまで、またはいずれかのシーケンスでエラーが発生するまで、マージされたシーケンス内の項目を生成し続けます。
例
次の例では、Interval 演算子を使用して、0 から始まる整数のシーケンスを作成します。 新しい整数は 500 ミリ秒ごとに生成されます。 フィルター処理された 2 つのシーケンスが作成されます。 1 つのシーケンスで元のシーケンスがフィルター処理され、3 番目の整数が生成されます。 もう 1 つのシーケンスでは、元のシーケンスがフィルター処理され、5 番目の整数ごとにのみ生成されます。 その後、Merge 演算子を使用して、これら 2 つのフィルター処理されたシーケンスを 1 つの整数シーケンスにマージします。 マージされたシーケンスのサブスクリプションが作成され、各項目がコンソール ウィンドウに書き込まれます。
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();
}
}
}
}
次の出力は、コード例によって生成されました。
Press ENTER to exit...
0
0
3
5
6
9
10
12
15
15
18
20
21
24
25
27
30
30