The preview 6 of dotnet 10 brings two more functions: InfiniteSequence
and Sequence
.
InfiniteSequence
InfiniteSequence
is a simple generator that allows you to get a sequence from a starting point via step size:
var steps = Enumerable.InfiniteSequence(0, 5)
.Take(10)
.ToList();
foreach (var step in steps)
{
Console.WriteLine(step);
}
This would print 0, 5, ... 45. So be careful not to call ToList
/Count
and friends without Take
and so on, otherwise you will be greeted by an "Out of memory" exception.
Sequence
That is almost the same as InfiniteSequence
with the difference that you can define the inclusive upper bound:
var steps = Enumerable.Sequence(0, 45, 5)
.ToList();
foreach (var step in steps)
{
Console.WriteLine(step);
}
The result would be the same as the InfiniteSequence
.
Update 22th of August: As mentioned by MizardX in the comments: Those methods are utilizing generic math and are defined like: T: IAdditionOperators<T,T,T>
so they would work with your custom objects as long as they inherit from that interface!