枚举捐赠意外输出 [英] Enumerable giving unexpected output

查看:162
本文介绍了枚举捐赠意外输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Foo
{
    public static IEnumerable<int> Range(int start, int end)
    {
        return Enumerable.Range(start, end);
    }

    public static void PrintRange(IEnumerable<int> r)
    {
        foreach (var item in r)
        {
            Console.Write(" {0} ", item);
        }
        Console.WriteLine();
    }
}

class Program
{
    static void TestFoo()
    {
        Foo.PrintRange(Foo.Range(10, 20));
    }

    static void Main()
    {
        TestFoo();
    }
}



期望输出:

Expected Output:

10  11  12  13  14  15  16  17  18  19  20

实际输出:

10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29

什么是这段代码的问题吗?发生了什么?

What is the problem with this code? Whats happening?

推荐答案

Enumerable.Range 指定整数产生的数量,的的范围内的最后一个整数。

The second parameter of Enumerable.Range specifies the number of integers to generate, not the last integer in the range.

如果有必要,它很容易建立自己的方法,或者更新现有的富.Range 的方法,来从一系列启动结束参数。

If necessary, it's easy enough to build your own method, or update your existing Foo.Range method, to generate a range from start and end parameters.

这篇关于枚举捐赠意外输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆