Thoughts on foreach with Enumerable.Range vs traditional for loop

前端 未结 18 1081
花落未央
花落未央 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:58

    I'd like to have the syntax of some other languages like Python, Haskell, etc.

    // Write the numbers 1 thru 7
    foreach (int index in [1..7])
    {
        Console.WriteLine(index);
    }
    

    Fortunatly, we got F# now :)

    As for C#, I'll have to stick with the Enumerable.Range method.

    0 讨论(0)
  • 2021-01-30 06:59

    This is just for fun. (I'd just use the standard "for (int i = 1; i <= 10; i++)" loop format myself.)

    foreach (int i in 1.To(10))
    {
        Console.WriteLine(i);    // 1,2,3,4,5,6,7,8,9,10
    }
    
    // ...
    
    public static IEnumerable<int> To(this int from, int to)
    {
        if (from < to)
        {
            while (from <= to)
            {
                yield return from++;
            }
        }
        else
        {
            while (from >= to)
            {
                yield return from--;
            }
        }
    }
    

    You could also add a Step extension method too:

    foreach (int i in 5.To(-9).Step(2))
    {
        Console.WriteLine(i);    // 5,3,1,-1,-3,-5,-7,-9
    }
    
    // ...
    
    public static IEnumerable<T> Step<T>(this IEnumerable<T> source, int step)
    {
        if (step == 0)
        {
            throw new ArgumentOutOfRangeException("step", "Param cannot be zero.");
        }
    
        return source.Where((x, i) => (i % step) == 0);
    }
    
    0 讨论(0)
  • 2021-01-30 07:04

    I think Range is useful for working with some range inline:

    var squares = Enumerable.Range(1, 7).Select(i => i * i);
    

    You can each over. Requires converting to list but keeps things compact when that's what you want.

    Enumerable.Range(1, 7).ToList().ForEach(i => Console.WriteLine(i));
    

    But other than for something like this, I'd use traditional for loop.

    0 讨论(0)
  • 2021-01-30 07:06

    It seems like quite a long winded approach to a problem that's already solved. There's a whole state machine behind the Enumerable.Range that isn't really needed.

    The traditional format is fundamental to development and familiar to all. I don't really see any advantage to your new style.

    0 讨论(0)
  • 2021-01-30 07:07

    I agree that in many (or even most cases) foreach is much more readable than a standard for-loop when simply iterating over a collection. However, your choice of using Enumerable.Range(index, count) isn't a strong example of the value of foreach over for.

    For a simple range starting from 1, Enumerable.Range(index, count) looks quite readable. However, if the range starts with a different index, it becomes less readable because you have to properly perform index + count - 1 to determine what the last element will be. For example…

    // Write the numbers 2 thru 8
    foreach (var index in Enumerable.Range( 2, 7 ))
    {
        Console.WriteLine(index);
    }
    

    In this case, I much prefer the second example.

    // Write the numbers 2 thru 8
    for (int index = 2; index <= 8; index++)
    {
        Console.WriteLine(index);
    }
    
    0 讨论(0)
  • 2021-01-30 07:08

    In my opinion the Enumerable.Range() way is more declarative. New and unfamiliar to people? Certainly. But I think this declarative approach yields the same benefits as most other LINQ-related language features.

    0 讨论(0)
提交回复
热议问题