locked
Why is IEnumerator GetEnumerator() a valid declaration in C#? RRS feed

  • Question

  • I'm studying C# right now and I know that methods declared inside an Interface must be implemented insde the class that derived the interface.

    A usual declaration would be something like this

    public void methodFromInterface()

    {

    //code here

    }

    but IEnumerator is an interface and GetEnumerator is a method and I assume that GetEnumerator is a method inside IEnumerable. The thing is why is it declared as:

    IEnumerator GetEnumerator()

    {

    //code here

    }

    I'm curious on why the Interface and the method name was used in such way unlike the first example which only the method name was used.

    Your answers would be of great help to me.


    Randel Ramirez
    Thursday, June 9, 2011 10:48 AM

Answers

  • Follow up:

    Is it because IEnumerator is being used a type here which means that the method requires an Ienumerator as a return value.


    Randel Ramirez
    • Marked as answer by Cookie Luo Tuesday, June 14, 2011 3:39 AM
    Thursday, June 9, 2011 10:55 AM
  • Hi

    Interface can contain methods, indexes, properties and events. methods can be with allowed signature.

     

    Implementer of interface IEnumerable must implement method declared GetEnumerator()

    and it should return type of IEnumerator

    for ex:

     public class Garage

        {

            public Cars CarsInGarage { get; set; }

        }

     

        public class Car

        {

        }

     

        public class Cars: IEnumerable

        {

            ArrayList listOfCars = new ArrayList();

            public void AddCar(Car car)

            {

                if (car != null)

                {

                    listOfCars.Add(car);

                }

            }

            public IEnumerator  GetEnumerator()

            {

                return listOfCars.GetEnumerator();

            }

        }

     

    Hope this helps you


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Thursday, June 9, 2011 2:19 PM

All replies

  • Follow up:

    Is it because IEnumerator is being used a type here which means that the method requires an Ienumerator as a return value.


    Randel Ramirez
    • Marked as answer by Cookie Luo Tuesday, June 14, 2011 3:39 AM
    Thursday, June 9, 2011 10:55 AM
  • Hello,

    When writing:

    IEnumerator GetEnumerator ()

    You´re just creating a method GetEnumerator without params which is RETURNING an object of type IEnumerator.


    Kind regards, Sebastien G. - Axiomtek Deutschland GmbH
    Thursday, June 9, 2011 10:57 AM
  • Hello,

    To understand that, you need to see a concrete example :

    check out the end of this tutorial : http://aelassas.free.fr/#post20

    Kind regards,


    aelassas.free.fr
    Thursday, June 9, 2011 12:37 PM
  • Hi

    Interface can contain methods, indexes, properties and events. methods can be with allowed signature.

     

    Implementer of interface IEnumerable must implement method declared GetEnumerator()

    and it should return type of IEnumerator

    for ex:

     public class Garage

        {

            public Cars CarsInGarage { get; set; }

        }

     

        public class Car

        {

        }

     

        public class Cars: IEnumerable

        {

            ArrayList listOfCars = new ArrayList();

            public void AddCar(Car car)

            {

                if (car != null)

                {

                    listOfCars.Add(car);

                }

            }

            public IEnumerator  GetEnumerator()

            {

                return listOfCars.GetEnumerator();

            }

        }

     

    Hope this helps you


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Thursday, June 9, 2011 2:19 PM