Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<T> in C#

Tags:

c#

ienumerable

I am trying to get the following code to compile but am getting errors in VS2008. Anyone can tell me where I am going wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dummy
{
    public class NaturalNumbersSequence : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
            for (int i = 1; i <= 1000; i++)
                yield return i;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            for (int i = 1; i <= 1000; i++)
                yield return i;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            foreach (int i in new NaturalNumbersSequence())
                Console.WriteLine(i);
        }
    }
}
like image 613
fcuk112 Avatar asked Jul 23 '09 21:07

fcuk112


2 Answers

Well, the first compiler error I get is that it complains that:

Using the generic type 'System.Collections.Generic.IEnumerator' requires '1' type arguments

This is on line 16, this one:

IEnumerator IEnumerable.GetEnumerator()

Fixing that by adding a using directive for the System.Collections namespace (tip: place the cursor just after IEnumerator, on the r at the end of the word, and hit Ctrl+. (ctrl + the dot-key), it should suggest you add a "using System.Collections;" directive, do that).

Then it compiles, and runs. Does that match what you expect?

Also, note that you should always post the actual error messages you're getting, this way we're not barking up the wrong tree if there's something else wrong with your code that we're not seeing at first glance.

Additionally, you can simplify this very common implementation of IEnumerable<T> by calling one of the methods from the other, hence I would simplify the implementation of the second methods like this:

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator(); // this will return the one
                            // available through the object reference
                            // ie. "IEnumerator<int> GetEnumerator"
}

this way you only implement the actual enumerator code once.

And finally see Earwicker's answer as well, it shows a better (in my opinion at least) way to write this whole code.

like image 72
Lasse V. Karlsen Avatar answered Sep 18 '22 01:09

Lasse V. Karlsen


Not sure why you're getting errors, but wouldn't this be simpler?

public static class NumbersSequence
{
    public static IEnumerable<int> Naturals
    {
        get 
        {
            for (int i = 1; i <= 1000; i++)
                yield return i;
        }
    }
}

class Program
{
    public static void Main(string[] args)
    {
        foreach (int i in NumbersSequence.Naturals)
            Console.WriteLine(i);
    }
}
like image 27
Daniel Earwicker Avatar answered Sep 22 '22 01:09

Daniel Earwicker