Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:21:17+00:00 2026-05-16T21:21:17+00:00

From MSDN, I got an example as follows: IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator();

  • 0

From MSDN, I got an example as follows:

IEnumerator IEnumerable.GetEnumerator()
{
   return (IEnumerator) GetEnumerator();
}

public PeopleEnum GetEnumerator()
{
    return new PeopleEnum(_people);
}

Why did the author do like this? Is it a best practice?

——–EDIT———

MSDN article link : http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx

Why didn’t the author do as follows:

 IEnumerator IEnumerable.GetEnumerator()
 {
        return new PeopleEnum(_people);
 }

I tried, it still works.

======= SECOND EDIT ==========

using System;
using System.Collections;

namespace MyEnumerableEnumerator
{
    class Person
    {
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public string Name { get; set; }
        public int Age { get; set; }
    }

    class PeopleEnumerator : IEnumerator
    {
        int Position { get; set; }
        Person[] People { get; set; }
        int Length { get; set; }

        public PeopleEnumerator(Person[] people)
        {
            People = people;
            Position = -1;
            Length = people.Length;
        }

        public bool MoveNext()
        {
            Position++;
            return Position < Length;
        }

        public void Reset()
        {
            Position = -1;
        }

        public object Current
        {
            get
            {
                try
                {
                    return People[Position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }

    class People : IEnumerable
    {
        Person[] Persons { get; set; }

        public People(Person[] people)
        {
            Persons = new Person[people.Length];

            for (int x = 0; x < people.Length; x++)
                Persons[x] = people[x];
        }

        //public IEnumerator GetEnumerator()
        //{
        //    return new PeopleEnumerator(Persons);
        //}

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private IEnumerator GetEnumerator()
        {
            return new PeopleEnumerator(Persons);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Person[] persons = { new Person("Mark", 1), new Person("Siva", 2) };

            People people = new People(persons);

            foreach (Person person in people)
            {
                Console.WriteLine("Name: {0}, Age: {1}", person.Name, person.Age);
            }

        }
    }
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:21:17+00:00Added an answer on May 16, 2026 at 9:21 pm

    They did as they did because they wanted a typed iterator for a non-generic collection.

    Edit

    foreach works since the compiler does some “magic”

    Try this with your changed code:

            var enumerator = peopleList.GetEnumerator();
            while (enumerator.MoveNext())
                Console.WriteLine(enumerator.Current.firstName); //compile error.
    

    It will not compile, you have to cast the enumerator to PeopleEnum or change back the return type.

    Edit 2

    You can read about foreach’s implicit type casting here:

    http://www.dev102.com/2009/07/28/pay-attention-to-the-foreach-implicit-casting/

    http://csharpindepth.com/ViewNote.aspx?NoteID=1

    And in the specification: http://msdn.microsoft.com/en-us/library/bb308966.aspx

    Scroll down to last section in 26.1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why in this example from MSDN, in GetEnumerator method, PeopleEnum returns IEnumerator ? public
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
From MSDN ( link ): The type PrintDocumentImageableArea has no public constructor. Hence, the
This is a repost from the msdn forums that got no good results. In
I can compile this code that I got from an MSDN page : using
I've got a property contained in a class for example public class Greeter {
I'm trying to implement a simple TPH example from http://msdn.microsoft.com/en-us/library/dd793152.aspx . I have two
I got this extract from msdn MSDN COMMAND BUILDER CLASS . Would it be
From MSDN doc : A delegate is a type that safely encapsulates a method,
From MSDN document i understand ( I am not sure,i understand perfectly ) :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.