共用方式為


Observable.Generate < TState, TResult > 方法 (TState, Func < TState, Boolean > , Func TState, TState, Func << TState > , TResult > , IScheduler)

在條件失敗之前,從初始狀態逐一查看狀態,以產生可觀察的序列。

Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (

Syntax

'Declaration
Public Shared Function Generate(Of TState, TResult) ( _
    initialState As TState, _
    condition As Func(Of TState, Boolean), _
    iterate As Func(Of TState, TState), _
    resultSelector As Func(Of TState, TResult), _
    scheduler As IScheduler _
) As IObservable(Of TResult)
'Usage
Dim initialState As TState
Dim condition As Func(Of TState, Boolean)
Dim iterate As Func(Of TState, TState)
Dim resultSelector As Func(Of TState, TResult)
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of TResult)

returnValue = Observable.Generate(initialState, _
    condition, iterate, resultSelector, _
    scheduler)
public static IObservable<TResult> Generate<TState, TResult>(
    TState initialState,
    Func<TState, bool> condition,
    Func<TState, TState> iterate,
    Func<TState, TResult> resultSelector,
    IScheduler scheduler
)
public:
generic<typename TState, typename TResult>
static IObservable<TResult>^ Generate(
    TState initialState, 
    Func<TState, bool>^ condition, 
    Func<TState, TState>^ iterate, 
    Func<TState, TResult>^ resultSelector, 
    IScheduler^ scheduler
)
static member Generate : 
        initialState:'TState * 
        condition:Func<'TState, bool> * 
        iterate:Func<'TState, 'TState> * 
        resultSelector:Func<'TState, 'TResult> * 
        scheduler:IScheduler -> IObservable<'TResult> 
JScript does not support generic types and methods.

類型參數

  • TState
    狀態的類型。
  • TResult
    結果的類型。

參數

  • initialState
    類型:TState
    初始狀態。
  • 逐一查看
    類型:System.Func< TState、TState>
    反復專案步驟函式。
  • resultSelector
    類型:System.Func< TState、TResult>
    序列中產生結果的選取器函式。

傳回值

類型:System.IObservable< TResult>
產生的序列。

備註

Generate 運算子會將反覆運算函式套用至 initialState,直到條件函式針對目前狀態傳回 false 為止,產生 TState 類型的序列。 resultSelector 函式會針對每個狀態執行,以在產生的序列中產生每個專案。

範例

此程式碼範例會使用 Generate 運算子來產生整數序列,其正方形小於 1000。

using System;
using System.Reactive.Concurrency;
using System.Reactive.Linq;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** Generate a sequence of integers which are the perfect squares that are less than 100. ***//
      //*********************************************************************************************//

      var obs = Observable.Generate(1,                      // Initial state value
                                    x => x * x < 1000,      // The termination condition. Terminate generation when false (the integer squared is not less than 1000).
                                    x => x + 1,             // Iteration step function updates the state and returns the new state. In this case state is incremented by 1.
                                    x => x * x,             // Selector function determines the next resulting value in the sequence. The state of type in is squared.
                                    Scheduler.ThreadPool);  // The ThreadPool scheduler runs the generation on a thread pool thread instead of the main thread.

      using (IDisposable handle = obs.Subscribe(x => Console.WriteLine(x)))
      {
        Console.WriteLine("Press ENTER to exit...\n");
        Console.ReadLine();
      }
    }
  }
}

下列輸出示範如何執行範例程式碼。

Press ENTER to exit...

1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729
784
841
900
961

另請參閱

參考

Observable 類別

產生多載

System.Reactive.Linq 命名空間