Export (0) Print
Expand All
Expand Minimize

Observable.ToEnumerable<TSource> Method

Converts an observable sequence to an enumerable sequence.

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

public static IEnumerable<TSource> ToEnumerable<TSource>(
	this IObservable<TSource> source
)

Type Parameters

TSource

The type of source.

Parameters

source
Type: System.IObservable<TSource>
An observable sequence to convert to an enumerable sequence.

Return Value

Type: System.Collections.Generic.IEnumerable<TSource>
The enumerable sequence containing the elements in the observable sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IObservable<TSource>. When you use instance method syntax to call this method, omit the first parameter. For more information, see http://msdn.microsoft.com/en-us/library/bb384936(v=vs.103).aspx or http://msdn.microsoft.com/en-us/library/bb383977(v=vs.103).aspx.

The ToEnumerator operator returns an enumerator from an observable sequence. The enumerator will yield each item in the sequence as it is produced.

The following example creates an observable sequence of the integers. A new integer is produced in the sequence every second by the Interval operator. The observable sequence is converted to an enumerator and each item is written to the console window as it is produced.

using System;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //******************************************************//
      //*** Create an observable sequence of integers.     ***//
      //******************************************************//

      var obs = Observable.Interval(TimeSpan.FromSeconds(1));
  

      //*******************************************************//
      //*** Convert the integer sequence to an enumerable.  ***//
      //*******************************************************//

      var intEnumerable = obs.ToEnumerable();


      //*********************************************************************************************//
      //*** Create a task to enumerate the items in the list on a worker thread to allow the main ***//
      //*** thread to process the user's ENTER key press.                                         ***//
      //*********************************************************************************************//

      Task.Factory.StartNew(() =>
      {
        foreach (int val in intEnumerable)
        {
          Console.WriteLine(val);
        }
      });


      //*********************************************************************************************//
      //*** Main thread waiting on the user's ENTER key press.                                    ***//
      //*********************************************************************************************//

      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }
  }
}

The following output was generated with the example code.

 
Press ENTER to exit...

0
1
2
3
4
5
6
7
8
9
Show:
© 2014 Microsoft