Skip to content

Instantly share code, notes, and snippets.

@mrange
Created May 16, 2024 05:39
Show Gist options
  • Save mrange/bd4cac25d4ebdb7296906c093450d527 to your computer and use it in GitHub Desktop.
Save mrange/bd4cac25d4ebdb7296906c093450d527 to your computer and use it in GitHub Desktop.
collection literals C#
// Gives an array of 3 elements
IEnumerable<ulong> x0 = Enumerable.Concat<ulong>([1], Unbounded());
ulong[] y0 = x0.Take(3).ToArray();
// Crashes with out of memory after some time
IEnumerable<ulong> x1 = [1, ..Unbounded()];
ulong[] y1 = x0.Take(3).ToArray();
IEnumerable<ulong> Unbounded()
{
ulong i = 0;
while(true)
{
++i;
Console.WriteLine($"Generated: {i}");
yield return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment