Created
February 26, 2021 11:36
-
-
Save olegmrzv/60b9c3733de1a356691cd4361af02e32 to your computer and use it in GitHub Desktop.
Range Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
// public class Program { | |
// public static void Main() { | |
// foreach(var i in 1..5) { | |
// Console.WriteLine(i); | |
// } | |
// } | |
// | |
// } | |
public static class RangeExtensions { | |
public struct RangeEnumerator : IEnumerator<int> { | |
private readonly int endValue; | |
public RangeEnumerator(int beginValue, int endValue) { | |
this.Current = --beginValue; | |
this.endValue = endValue; | |
} | |
public int Current { get; private set; } | |
object IEnumerator.Current => this.Current; | |
public bool MoveNext() => ++this.Current <= this.endValue; | |
public void Reset() {} | |
public void Dispose() {} | |
} | |
public static RangeEnumerator GetEnumerator(this Range range) | |
=> new RangeEnumerator(range.Start.Value, range.End.Value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment