IEnumerable reports compiler error when used with the namespace System.Collections

倾然丶 夕夏残阳落幕 提交于 2019-12-24 16:04:58

问题


I was looking at this Squares extension method which was already there in Internet. I could not get this compiling. The compiler reports something like, "The non-generic type `System.Collections.IEnumerable' cannot be used with the type arguments".

any ideas what is wrong with this code below ?

any help is much appreciated.

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);
        }
    }
}

回答1:


Replace using System.Collections; with using System.Collections.Generic;.



来源:https://stackoverflow.com/questions/19398358/ienumerable-reports-compiler-error-when-used-with-the-namespace-system-collectio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!