Skip to content

Commit 9a84009

Browse files
Update documentation for Defer
1 parent d578f55 commit 9a84009

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
uid: SuperLinq.SuperEnumerable.Defer``1(System.Func{System.Collections.Generic.IEnumerable{``0}})
3+
example: [*content]
4+
---
5+
The following code example demonstrates how to defer the execution of a method that returns an enumerable using `Defer`.
6+
[!code-csharp[](SuperLinq/Defer/Defer.linq#L6-)]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var count = 3;
7+
8+
// Use a function to create a sequence at execution time
9+
var result = SuperEnumerable
10+
.Defer(() => Enumerable.Range(1, count));
11+
12+
Console.WriteLine(
13+
"[" +
14+
string.Join(", ", result) +
15+
"]");
16+
17+
// changing count changes the length of the sequence
18+
// returned by the function given to Defer
19+
count = 5;
20+
21+
Console.WriteLine(
22+
"[" +
23+
string.Join(", ", result) +
24+
"]");
25+
26+
// This code produces the following output:
27+
// [1, 2, 3]
28+
// [1, 2, 3, 4, 5]

Source/SuperLinq/Defer.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,36 @@ namespace SuperLinq;
66
public static partial class SuperEnumerable
77
{
88
/// <summary>
9-
/// Creates an enumerable sequence based on an enumerable factory function.
9+
/// Creates an enumerable sequence based on an enumerable factory function.
1010
/// </summary>
11-
/// <typeparam name="TResult">Result sequence element type.</typeparam>
12-
/// <param name="enumerableFactory">Enumerable factory function.</param>
13-
/// <returns>Sequence that will invoke the enumerable factory upon iteration.</returns>
14-
/// <exception cref="ArgumentNullException"><paramref name="enumerableFactory"/> is <see
15-
/// langword="null"/>.</exception>
11+
/// <typeparam name="TResult">
12+
/// Result sequence element type.
13+
/// </typeparam>
14+
/// <param name="enumerableFactory">
15+
/// Enumerable factory function.
16+
/// </param>
17+
/// <returns>
18+
/// Sequence that will invoke the enumerable factory upon iteration.
19+
/// </returns>
20+
/// <exception cref="ArgumentNullException">
21+
/// <paramref name="enumerableFactory"/> is <see langword="null"/>.
22+
/// </exception>
23+
/// <exception cref="ArgumentNullException">
24+
/// (Thrown lazily) The sequence <c>source</c> returned by <paramref name="enumerableFactory"/> is <see
25+
/// langword="null"/>.
26+
/// </exception>
27+
/// <remarks>
28+
/// <para>
29+
/// <paramref name="enumerableFactory"/> is not run until the sequence returned by <see
30+
/// cref="Defer{TResult}(Func{IEnumerable{TResult}})"/> is enumerated. At enumeration, <paramref
31+
/// name="enumerableFactory"/> is executed and the sequence returned is enumerated in a streaming manner and
32+
/// values are returned similarly.
33+
/// </para>
34+
/// <para>
35+
/// <paramref name="enumerableFactory"/> is executed each time the sequence returned by <see
36+
/// cref="Defer{TResult}(Func{IEnumerable{TResult}})"/> is enumerated.
37+
/// </para>
38+
/// </remarks>
1639
public static IEnumerable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)
1740
{
1841
Guard.IsNotNull(enumerableFactory);
@@ -21,7 +44,10 @@ public static IEnumerable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enu
2144

2245
static IEnumerable<TResult> Core(Func<IEnumerable<TResult>> enumerableFactory)
2346
{
24-
foreach (var el in enumerableFactory())
47+
var source = enumerableFactory();
48+
Guard.IsNotNull(source);
49+
50+
foreach (var el in source)
2551
yield return el;
2652
}
2753
}

0 commit comments

Comments
 (0)