Observable.ToList<TSource> メソッド
監視可能なシーケンスからリストを作成します。
Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)
構文
'Declaration
<ExtensionAttribute> _
Public Shared Function ToList(Of TSource) ( _
source As IObservable(Of TSource) _
) As IObservable(Of IList(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim returnValue As IObservable(Of IList(Of TSource))
returnValue = source.ToList()
public static IObservable<IList<TSource>> ToList<TSource>(
this IObservable<TSource> source
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<IList<TSource>^>^ ToList(
IObservable<TSource>^ source
)
static member ToList :
source:IObservable<'TSource> -> IObservable<IList<'TSource>>
JScript does not support generic types and methods.
型パラメーター
- TSource
ソースの種類。
パラメーター
- source
種類: System.IObservable<TSource>
要素の一覧を取得するソース監視可能なシーケンス。
戻り値
種類: System.IObservable<IList<TSource>>
監視可能なシーケンスからのリスト。
使用上の注意
Visual Basic および C# では、 IObservable<TSource> 型の任意のオブジェクトでインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。
解説
ToList 演算子は、シーケンス内のすべての項目を受け取り、リストに配置します。 その後、リストは監視可能なシーケンス (IObservable<IList<TSource>>) として返されます。 この演算子の戻り値は、非同期動作を保持するために、IEnumerable の対応する演算子とは異なります。
例
次の例では、Generate 演算子を使用して整数の単純なシーケンス (1 から 10) を生成します。 次に、ToList 演算子を使用して、そのシーケンスをリストに変換します。 IList.Add メソッドは、リスト内の各項目がコンソール ウィンドウに書き込まれる前に、結果のリストに対して 9999 に使用されます。
using System;
using System.Reactive.Linq;
using System.Collections;
namespace Example
{
class Program
{
static void Main()
{
//*********************************************//
//*** Generate a sequence of integers 1-10 ***//
//*********************************************//
var obs = Observable.Generate(1, // Initial state value
x => x <= 10, // The termination condition. Terminate generation when false (the integer squared is not less than 1000)
x => ++x, // Iteration step function updates the state and returns the new state. In this case state is incremented by 1
x => x); // Selector function determines the next resulting value in the sequence. The state of type in is squared.
//***************************************************************************************************//
//*** Convert the integer sequence to a list. Use the IList.Add() method to add 9999 to the list ***//
//***************************************************************************************************//
var obsList = obs.ToList();
obsList.Subscribe(x =>
{
x.Add(9999);
//****************************************//
//*** Enumerate the items in the list ***//
//****************************************//
foreach (int val in x)
{
Console.WriteLine(val);
}
});
Console.WriteLine("\nPress ENTER to exit...\n");
Console.ReadLine();
}
}
}
コード例を使用して、次の出力が生成されました。
1
2
3
4
5
6
7
8
9
10
9999
Press ENTER to exit...