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 4608830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:03+00:00 2026-05-22T00:54:03+00:00

I assume the following sample gives a best practice that we should follow when

  • 0

I assume the following sample gives a best practice that we should follow when we implement the IEnumerable interface.

https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerator.movenext

Here is the question:

  1. Why should we provide two version of Current method?
  2. When the version ONE (object IEnumerator.Current) is used?
  3. When the version TWO (public Person Current ) is used?
  4. How to use PeopleEnum in the foreach statement. // updated
public class PeopleEnum : IEnumerator
{
    public Person[] _people;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public PeopleEnum(Person[] list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Length);
    }

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

    // explicit interface implementation
    object IEnumerator.Current /// **version ONE**
    {
        get
        {
            return Current;
        }
    }

    public Person Current     /// **version TWO**
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 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-22T00:54:03+00:00Added an answer on May 22, 2026 at 12:54 am

    I suspect the reason is that this code example was derived from an example class implementing IEnumerator<T> – if the example class PeopleEnum implemented IEnumerator<T> this approach would be required: IEnumerator<T> inherits IEnumerator so you have to implement both interfaces when implementing IEnumerator<T>.

    The implementation of the non-generic IEnumerator requires Current to return object – the strongly typed IEnumerator<T> on the other hand requires Current to return an instance of type T – using explicit and direct interface implementation is the only way to fulfill both requirements.

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

Sidebar

Related Questions

Assume the following function prototype: void *function(int arg) . Is that a pointer to
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
Assume the following simple table design: Table Master (Id bigint PK, Message nvarchar(50)) Sample
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Assume the following scenario: All data are in Sql Server tables. 1) An Execute
Assume the following situation <div> <div1> <divA> </divA> <divB> </divB> </div1> <div2> </div2> </div>
Assume the following scenario. I have two files A.cpp and B.cpp in trunk. At
assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return
Assume the following: we have class B, which is a private class nested inside
If I use a lambda expression like the following // assume sch_id is a

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.