Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Why implement two version of Current when realizing IEnumerable Interface?

Tags:

c#

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

https://docs.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();
            }
        }
    }
}
like image 956
q0987 Avatar asked May 06 '11 20:05

q0987


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

The IEnumerator.Current is an explicit interface implementation.

You can only use it if you cast the iterator to an IEnumerator (which is what the framework does with foreach). In other cases, the second version will be used.

You will see that it returns object and actually uses the other implementation which returns a Person.

The second implementation is not required per se by the interface, but is there as a convenience and in order to return the expected type instead of object.

like image 116
Oded Avatar answered Oct 08 '22 15:10

Oded