Why are there private methods for interface implementation???

R

Rene

Hi.

Before I start, I would like to point out that in order for you to follow my
question you may need to use Reflector.

Ok, so here is the deal. I was messing around with Reflector checking out
some code when I stumbled across a class called
"OrderedEnumerable<TElement>".

If you look at the class using Reflector, you will notice that the class
contains a nested class called "<GetEnumerator>d_0". If you in turn, look at
this "<GetEnumerator>d_0" class, you will notice that it implements the
"IEnumerator" interface.

Now, I realize that most people are well aware of what methods the
"IEnumerator" interface implements but I will list them here anyway:

public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}

Notice how the "IEnumerator" requires the implementation of the "MoveNext()"
method.

Now if you don't mind, I would like you to look at the "<GetEnumerator>d_0"
class and tell me where is this "MoveNext()" method implemented is such a
way so that it satisfies the requirements of implementing the "IEnumerator"
interface.

You will notice that there is a "MoveNext()" method implement but its
implemented a *private* and as far as I was aware of, the implementation
should be *public* or *explicit* in order for the compiler to allow this to
compile.

So why did the complier allowed this to happen? What am I missing? Something
tells me that is going to be something very obvious and it's a stupid
question but I just can't see what's going on!

Thanks for your help.
 
J

Jeroen Mostert

Rene said:
Before I start, I would like to point out that in order for you to
follow my question you may need to use Reflector.

Ok, so here is the deal. I was messing around with Reflector checking
out some code when I stumbled across a class called
"OrderedEnumerable<TElement>".

If you look at the class using Reflector, you will notice that the class
contains a nested class called "<GetEnumerator>d_0". If you in turn,
look at this "<GetEnumerator>d_0" class, you will notice that it
implements the "IEnumerator" interface.

Now, I realize that most people are well aware of what methods the
"IEnumerator" interface implements but I will list them here anyway:

public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}

Notice how the "IEnumerator" requires the implementation of the
"MoveNext()" method.

Now if you don't mind, I would like you to look at the
"<GetEnumerator>d_0" class and tell me where is this "MoveNext()" method
implemented is such a way so that it satisfies the requirements of
implementing the "IEnumerator" interface.

You will notice that there is a "MoveNext()" method implement but its
implemented a *private* and as far as I was aware of, the implementation
should be *public* or *explicit* in order for the compiler to allow this
to compile.
You're right: you can't write this code in C#. But what you're seeing wasn't
written in C#, it was produced automatically by the C# compiler, which only
has to play by the rules and restrictions of IL. In IL, you can implement an
interface method with a private method that's named whatever you like, and
that's exactly what happens here:

.method private hidebysig newslot virtual final instance bool
MoveNext() cil managed
{
.override [mscorlib]System.Collections.IEnumerator::MoveNext
}

This is actually even possible in C# if you use explicit interface
implementation: http://msdn.microsoft.com/library/ms173157

In C#, however, this means the method must be given a specific name: it
would have to be called System.Collections.IEnumerator.MoveNext(). Since all
the other methods of the class do have explicit names, I'm guessing this one
is an oversight: there's no particular benefit to calling it just MoveNext().
 
R

Rene

Thanks Jeroen.

Jeroen Mostert said:
Rene said:
Before I start, I would like to point out that in order for you to follow
my question you may need to use Reflector.

Ok, so here is the deal. I was messing around with Reflector checking out
some code when I stumbled across a class called
"OrderedEnumerable<TElement>".

If you look at the class using Reflector, you will notice that the class
contains a nested class called "<GetEnumerator>d_0". If you in turn, look
at this "<GetEnumerator>d_0" class, you will notice that it implements
the "IEnumerator" interface.

Now, I realize that most people are well aware of what methods the
"IEnumerator" interface implements but I will list them here anyway:

public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}

Notice how the "IEnumerator" requires the implementation of the
"MoveNext()" method.

Now if you don't mind, I would like you to look at the
"<GetEnumerator>d_0" class and tell me where is this "MoveNext()" method
implemented is such a way so that it satisfies the requirements of
implementing the "IEnumerator" interface.

You will notice that there is a "MoveNext()" method implement but its
implemented a *private* and as far as I was aware of, the implementation
should be *public* or *explicit* in order for the compiler to allow this
to compile.
You're right: you can't write this code in C#. But what you're seeing
wasn't written in C#, it was produced automatically by the C# compiler,
which only has to play by the rules and restrictions of IL. In IL, you can
implement an interface method with a private method that's named whatever
you like, and that's exactly what happens here:

.method private hidebysig newslot virtual final instance bool
MoveNext() cil managed
{
.override [mscorlib]System.Collections.IEnumerator::MoveNext
}

This is actually even possible in C# if you use explicit interface
implementation: http://msdn.microsoft.com/library/ms173157

In C#, however, this means the method must be given a specific name: it
would have to be called System.Collections.IEnumerator.MoveNext(). Since
all the other methods of the class do have explicit names, I'm guessing
this one is an oversight: there's no particular benefit to calling it just
MoveNext().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top