integer range collection expression

μ˜ˆμ „μ— λΈ”λ‘œκ·Έμ— 올렸던 λ‚΄μš©μž…λ‹ˆλ‹€.

μ‚¬μš©μ½”λ“œ:

foreach(var n in 0..10)
{
   //...
}

foreach(var n in ..10)
{
   //...
}

κ΅¬ν˜„ μ½”λ“œ :

public ref struct RangeEnumerator 
{
    private int _current;
    private int _end;

    public int Current => _current++;
    public bool MoveNext() => _current < _end;

    public RangeEnumerator(Range range)
    {
        _current = range.Start.Value;
        _end = range.End.Value;
    }
}
public static class RangeExtentions
{
    public static RangeEnumerator GetEnumerator(this Range range)
    {
        return new RangeEnumerator(range);
    } 
}
3 Likes

νŠœν”Œλ„ μ„žμ–΄κ΅¬ν˜„ν• μˆ˜λ„ μžˆκ² λ„€μš” γ…‹γ…‹

foreach(var n in (0..10, 0.2f))
{
   //...
}
2 Likes