Thoughts on foreach with Enumerable.Range vs traditional for loop

前端 未结 18 1248
花落未央
花落未央 2021-01-30 06:13

In C# 3.0, I\'m liking this style:

// Write the numbers 1 thru 7
foreach (int index in Enumerable.Range( 1, 7 ))
{
    Console.WriteLine(index);
}
18条回答
  •  难免孤独
    2021-01-30 06:53

    Just throwing my hat into the ring.

    I define this...

    namespace CustomRanges {
    
        public record IntRange(int From, int Thru, int step = 1) : IEnumerable {
    
            public IEnumerator GetEnumerator() {
                for (var i = From; i <= Thru; i += step)
                    yield return i;
            }
    
            IEnumerator IEnumerable.GetEnumerator()
                => GetEnumerator();
        };
    
        public static class Definitions {
    
            public static IntRange FromTo(int from, int to, int step = 1)
                => new IntRange(from, to - 1, step);
    
            public static IntRange FromThru(int from, int thru, int step = 1)
                => new IntRange(from, thru, step);
    
            public static IntRange CountFrom(int from, int count)
                => new IntRange(from, from + count - 1);
    
            public static IntRange Count(int count)
                => new IntRange(0, count);
    
            // Add more to suit your needs. For instance, you could add in reversing ranges, etc.
        }
    }
    

    Then anywhere I want to use it, I add this at the top of the file...

    using static CustomRanges.Definitions;
    

    And use it like this...

    foreach(var index in FromTo(1, 4))
        Debug.WriteLine(index);
    // Prints 1, 2, 3
    
    foreach(var index in FromThru(1, 4))
        Debug.WriteLine(index);
    // Prints 1, 2, 3, 4
    
    foreach(var index in FromThru(2, 10, 2))
        Debug.WriteLine(index);
    // Prints 2, 4, 6, 8, 10
    
    foreach(var index in CountFrom(7, 4))
        Debug.WriteLine(index);
    // Prints 7, 8, 9, 10
    
    foreach(var index in Count(5))
        Debug.WriteLine(index);
    // Prints 0, 1, 2, 3, 4
    
    foreach(var _ in Count(4))
        Debug.WriteLine("A");
    // Prints A, A, A, A
    

    The nice thing about this approach is by the names, you know exactly if the end is included or not.

提交回复
热议问题