views:

156

answers:

5

I often have to implement some interfaces such as IEnumerable<T> in my code.

Each time, when implementing automatically, I encounter the following:

public IEnumerator<T> GetEnumerator() {
    // Code here...
}

public IEnumerator GetEnumerator1() {
    // Code here...
}

Though I have to implement both GetEnumerator() methods, they impossibly can have the same name, even if we understand that they do the same, somehow. The compiler can't treat them as one being the overload of the other, because only the return type differs.

When doing so, I manage to set the GetEnumerator1() accessor to private. This way, the compiler doesn't complaint about not implementing the interface member, and I simply throw a NotImplementedException within the method's body.

However, I wonder whether it is a good practice, or if I shall proceed differently, as perhaps a method alias or something like so.

What is the best approach while implementing an interface such as IEnumerable<T> that requires the implementation of two different methods with the same name?

EDIT #1

Does VB.NET reacts differently from C# while implementing interfaces, since in VB.NET it is explicitly implemented, thus forcing the GetEnumerator1(). Here's the code:

Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
    // Code here...
End Function

Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator Implements System.Collections.Generic.IEnumerable.GetEnumerator
    // Code here...
End Function

Both GetEnumerator() methods are explicitly implemented, and the compile will refuse them to have the same name. Why?

+6  A: 

You can use explicit interface implementation:

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

public IEnumerator<T> GetEnumerator()
{
    ...
}
Bryan Watts
Please my question edit.
Will Marcouiller
+1  A: 

You should be able to use Explicit Interface Implementations to create the two methods that have the same signature. Depending on what you are enumerating, I would just pass these calls through to an internal IEnumerable<T> such as a List or array.

John Bledsoe
Please my question edit.
Will Marcouiller
+1  A: 

In Visual Basic, all interface implementations are explicit.

Interface mappings are defined by the Implements statement so you can name your interface implementation methods whatever you want. (Unlike C#, where the compiler infers which methods implement interfaces by matching their names and signatures.)

Changing the method name and visibility (as appropriate) is standard practice in VB. See Implementing Interfaces in VB.NET for a good overview.

Jeff Sternal
Please my question edit.
Will Marcouiller
I agree, but even, the compiler still complaint if I rename `GetEnumerator1()` to `GetEnumerator()`, as it has already a method named `GetEnumerator()`. What is the best solution/approach favorited by .NET in such scenarios? Thanks! =)
Will Marcouiller
A: 

Implementing the non-generic interface explicitly allows both methods to have the same name, and allows the non-generic version to be implemented in terms of the generic one. Along the lines of:

public class TestEnumerable : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        // Type-safe implementation here.
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
Cumbayah
Please my question edit.
Will Marcouiller
A: 

Will, did you ever get a answer to this. It appears that it is different between C# and VB, however the above responses seem to have ignored that part of the question. I hate having to have a GetEnumerator1 in my class, it looks ugly and is unintuative. Did you get around it?

Clint Colefax
Thanks for your concern about my question, Clint Colefax. Although this should have been a comment, I appreciate your intervention. Nevertheless, I had to make my `GetEnumerator1()` method implementation `Private`. Now, if you ask whether I am satisfied with the solution, not really. Like you, I hate having a `GetEnumerator1()` in my class. Any better suggestion than making `Private` or renaming it is welcome. Thanks in advance whatever your solution is. =)
Will Marcouiller