다음을 통해 공유


Observable.ToList<TSource> 메서드

관찰 가능한 시퀀스에서 목록을 만듭니다.

네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)

Syntax

'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> 형식의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다<. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 또는 를 참조하세요.

설명

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...

참고 항목

참조

관찰 가능한 클래스

System.Reactive.Linq 네임스페이스