0

How can loop through this list through a foreach loop I get an error saying does not contain a public instance definition for GetEnumerator

    public class GenericList<T>
    {
        public void Add(T use) { }

    }       
    static void Main(string[] args)
    {
        GenericList<int> list1 = new GenericList<int>();
        list1.Add(1);
        list1.Add(2);
        list1.Add(3);
        list1.Add(4);
        list1.Add(5);
       foreach(int i in list1)
        {
            Console.WriteLine(i);
        }
  • 4
    Possible duplicate of [How would you implement the IEnumerator interface?](https://stackoverflow.com/questions/53967/how-would-you-implement-the-ienumerator-interface) – ProgrammingLlama Mar 01 '19 at 01:59
  • 2
    why are you recreating a list, when there is a generic list allready for you in the BCL – TheGeneral Mar 01 '19 at 01:59
  • 1
    [Alternative duplicate](https://stackoverflow.com/questions/6908373/c-sharp-how-to-implement-ienumerator-on-a-class) – ProgrammingLlama Mar 01 '19 at 02:00
  • `var mylist = new List();` – TheGeneral Mar 01 '19 at 02:05
  • 1
    Just by calling a class as List will not make it a list. You will have to implement features for the list (a backing store..lets say an array, and other methods). Once you create the other methods, you will see what you are missing. – peeyush singh Mar 01 '19 at 02:05
  • 1
    You should really talk to your teacher about what you are trying to do, because you have probably missed something very important – TheGeneral Mar 01 '19 at 02:06
  • If you are using Visual Studio, make your list class implement `IList`. At that point VS will tell you everything that an IList requires (publicly). Things like Add and both a generic and non-generic enumerator capability. You'll still need to have a place to store your data (to start with, use a private `List`). For the enumerators, look up C#'s *"yield return"* – Flydog57 Mar 01 '19 at 02:19

1 Answers1

0

So generally just use List, meaning your code should just be:

static void Main(string[] args)
    {
        var list1 = new List<int>();
        list1.Add(1);
        list1.Add(2);
        list1.Add(3);
        list1.Add(4);
        list1.Add(5);
       foreach(int i in list1)
        {
            Console.WriteLine(i);
        }

However, what I am guessing you are asking is what is required to use foreach on your own type.

The answer is a bit of compiler magic of duck typing. If your iteration class implements a method called GetEnumerator() returning a type complying with certain rules (a MoveNext method taking no parameters and returning a bool and a Current Property you are good to go, see the most useless iterator ever below:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var x= new MyIterable();

            foreach (var item in x)
            {
                Console.WriteLine(item);
            }
        }
    }

    public class MyIterable
    {
        public Item Current { get; set; }

        public bool MoveNext()
        {
            return false;
        }

        public MyIterable GetEnumerator()
        {
            return this;
        }
    }

    public class Item
    {
    }

See https://blogs.msdn.microsoft.com/kcwalina/2007/07/18/duck-notation/

Mirko
  • 4,284
  • 1
  • 22
  • 19
  • Not at all it is really hard sometimes to figure out how to help so if I did at all I am happy! – Mirko Mar 01 '19 at 05:35