1

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);
        }
    }
}
3
  • Your terminology is a bit off here. What you are getting is a compiler error and not an exception. Exceptions happen at runtime. Oct 16, 2013 at 8:30
  • @DanielHilgarth: Yup, you are right. Edited :) Oct 16, 2013 at 8:33
  • 1
    IEnumerable is a type and not a method and hence has no return type there's a different type IEnumerable<T> which is a generic type with one type argument
    – Rune FS
    Oct 16, 2013 at 8:37

1 Answer 1

13

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

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.