Interface that's not an interface?

J

James A. Fortune

From the C# Language Specification 4.0, Section 1.9 Interfaces:

"For example, the EditBox class could implement the IControl.Paint and
IDataBound.Bind methods using explicit interface member
implementations as follows.

public class EditBox : IControl, IDataBound
{
void IControl.Paint() {...}
void IDataBound.Bind(Binder b) {...}
}

Explicit interface members can only be accessed via the interface
type. For example, the implementation of IControl.Paint provided by
the previous EditBox class can only be invoked by first converting the
EditBox reference to the IControl interface type.

EditBox editBox = new EditBox();
editBox.Paint(); // Error; no such method
IControl control = editBox;
control.Paint(); // Okay"

VLADIMIR RESHETNIKOV: Actually, explicitly implemented interface
members can also be accessed via a type parameter, constrained to the
interface type.

For some reason, my current C# vocabulary does not allow me to
understand Vladimir's comment fully. It's probably quite simple.
Would someone mind showing me a code example of a type parameter,
constrained to the interface type, or give an explanation of the gist
of his comment?

Thanks,

James A. Fortune
(e-mail address removed)
 
A

Arne Vajhøj

From the C# Language Specification 4.0, Section 1.9 Interfaces:

"For example, the EditBox class could implement the IControl.Paint and
IDataBound.Bind methods using explicit interface member
implementations as follows.

public class EditBox : IControl, IDataBound
{
void IControl.Paint() {...}
void IDataBound.Bind(Binder b) {...}
}

Explicit interface members can only be accessed via the interface
type. For example, the implementation of IControl.Paint provided by
the previous EditBox class can only be invoked by first converting the
EditBox reference to the IControl interface type.

EditBox editBox = new EditBox();
editBox.Paint(); // Error; no such method
IControl control = editBox;
control.Paint(); // Okay"

VLADIMIR RESHETNIKOV: Actually, explicitly implemented interface
members can also be accessed via a type parameter, constrained to the
interface type.

For some reason, my current C# vocabulary does not allow me to
understand Vladimir's comment fully. It's probably quite simple.
Would someone mind showing me a code example of a type parameter,
constrained to the interface type, or give an explanation of the gist
of his comment?

I assume that he means something like:

public class Foo<T> where T : IControl
{
public void Bar(T o)
{
o.Paint();
}
}

Arne
 
J

James A. Fortune

On 8/26/2011 5:01 PM, James A. Fortune wrote:







I assume that he means something like:

public class Foo<T> where T : IControl
{
     public void Bar(T o)
     {
          o.Paint();
     }

}

Arne

Thanks Arne. As they say in Beverly, MA (where I lived for a few
years), "Light breaks over Marblehead." :).

James A. Fortune
(e-mail address removed)
 

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