Method Generate
| Edit this page View SourceGenerate<TResult>(TResult, Func<TResult, TResult>)
Returns a sequence of values consecutively generated by a generator function.
Declaration
public static IEnumerable<TResult> Generate<TResult>(TResult initial, Func<TResult, TResult> generator)
Parameters
Type | Name | Description |
---|---|---|
TResult | initial | Value of first element in sequence |
Func<TResult, TResult> | generator | Generator function which takes the previous series element and uses it to generate the next element. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence containing the generated values. |
Type Parameters
Name | Description |
---|---|
TResult | Type of elements to generate. |
Remarks
This function defers element generation until needed and streams the results. It is treated as an infinite sequence, and will not terminate.
Examples
The following code example demonstrates how to generate a sequence from a generator function using Generate
.
// Generate a sequence using a generator function
var result = SuperEnumerable
.Generate(1, n => n * 2)
.Take(10);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|