Skip to content

Instantly share code, notes, and snippets.

@olegmrzv
Created February 26, 2021 11:36
Show Gist options
  • Save olegmrzv/60b9c3733de1a356691cd4361af02e32 to your computer and use it in GitHub Desktop.
Save olegmrzv/60b9c3733de1a356691cd4361af02e32 to your computer and use it in GitHub Desktop.
Range Extensions
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