Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need two Current methods for IEnumerator<T> in C#?

Tags:

c#

oop

interface

I'm writing a class that implements the interface IEnumerator. To get it to compile, I need the following two methods:

public object Current
{
    get { return current; }
}

T IEnumerator<T>.Current
{
    get { return current; }
}

Why are both of these necessary? On the surface, it looks like only the latter is needed.

like image 409
Ben Hamner Avatar asked Jan 30 '26 07:01

Ben Hamner


1 Answers

That's because IEnumerator<T> implements the IEnumerator interface which is the non-generic version of the interface that existed before generics were introduced in .NET 2.0. So when implementing an interface (IEnumerator<T> in your case) you need to also implement all other interfaces that this interface implements.

like image 164
Darin Dimitrov Avatar answered Feb 01 '26 21:02

Darin Dimitrov