与命名空间System.Collections一起使用时,IEnumerable报告编译器错误

now*_*ed. 1 .net c# extension-methods .net-4.0

我正在看这个Squares已经存在于Internet中的扩展方法.我无法得到这个编译.编译器报告类似"非泛型类型`System.Collections.IEnumerable'不能与类型参数一起使用".

任何想法下面的代码有什么问题?

任何帮助深表感谢.

using System.IO;
using System;
using System.Collections;

static class Program {

     static IEnumerable<int> Squares (this int from, int to) {
        for (int i=from;i<=to;i++)
        {
            yield return (int)i*i;
        }
    }

    static void Main(string[] args)
    {
        var min=1;
        foreach (int i in min.Squares(4))
        {
            Console.WriteLine(i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Med*_*noc 13

替换using System.Collections;using System.Collections.Generic;.

  • @nowhewhomustnotbenamed.:因为有两个接口:非泛型[`System.Collections.IEnumerable`](http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx)和generic [`System.Collections.Generic.IEnumerable <T>`](http://msdn.microsoft.com/en-us/library/9eekhta0.aspx). (2认同)