-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Range.AsEnumerable() extension #762
Comments
This would belong in a more general library of extension methods or even one specific for foreach (var x in 1..10)
Console.WriteLine(x);
static class RangeExtensions
{
public static IEnumerator<int> GetEnumerator(this Range range)
{
var max = (long)int.MaxValue + 1;
long Normalize(Index i) => i.IsFromEnd ? max - i.Value : i.Value;
var (start, end) = (Normalize(range.Start), Normalize(range.End));
for (var i = start; i < end; i++)
yield return checked((int)i);
}
} The above will print numbers from 1 to 9. It won't be simply interchangeable with Putting all that aside, |
You might be interested in checking out #748 where a more appropriate use of |
Currently, we can create a sequence using a static method:
However, it would be nice if we could instead start with a C# construct like:
I propose adding:
The text was updated successfully, but these errors were encountered: