Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explicit interface implementation, why explicit casting

Tags:

c#

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

(This example is taken from here: MSDN: Explicit Interface Implementation)

You have two interfaces as follows.

interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}

And you implement them explicitly.

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

Now, to use the interfaces you have the following code.

// Call the Paint methods from Main.

SampleClass obj = new SampleClass();
//obj.Paint();  // Compiler error.

IControl c = (IControl)obj;
c.Paint();  // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass. 

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

?

The reason for my confusion is because, for example, you can do the following

IDictionary<string, string> c = new Dictionary<string, string>();

without explicitly casting new Dictionary to IDictionary.

Thanks.

like image 380
nomad Avatar asked Nov 06 '13 15:11

nomad


People also ask

What is implicit and explicit interface implementation in C#?

An implicit interface implementation is where you have a method with the same signature of the interface. An explicit interface implementation is where you explicitly declare which interface the method belongs to.

Why are go interfaces implicit?

A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. Implicit interfaces decouple the definition of an interface from its implementation, which could then appear in any package without prearrangement.

When an interface inherits another interface the implementing class must?

When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.

What happens when we call a virtual method via an interface?

A virtual (concrete) method declared in an interface may be overridden to be abstract in a derived interface. abstract void IBaseInterface. BaseInterfaceDefaultVirtualMethod(); But remember if you are overriding any base virtual member as abstract then you can't provide any definition at all.


2 Answers

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

The member effectively doesn't exist on the class, as far as the compiler's concerned - it only exists on the interface. You don't have to explicitly cast though - you just have to have a reference which has a compile-time type of the interface. That can be done however you like, including implicit conversions.

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

You don't have to. The implicit conversion should be absolutely fine. You would have to cast explicitly in order to call the method in a single expression, e.g.

obj.Paint(); // Invalid
((IControl) obj).Paint(); // Valid

But if you go through an implicit conversion via assignment to a separate local variable of the interface type, that's fine - the method is still being called with a target expression which is of the interface type.

like image 190
Jon Skeet Avatar answered Sep 25 '22 04:09

Jon Skeet


Explicit Interface Implementation is required only when a type inherits from multiple interfaces and some of the methods have same name/signature in more than one interfaces.

Rest it is matter of preference, and convention.

mpleClass obj = new SampleClass();
//obj.Paint();  // Compiler error. 

obj.Paint() --> This is error as When Explicit Interface implementation is done, the underlying interface implementation requires explicit cast as specified in MSDN

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

like image 27
Tilak Avatar answered Sep 26 '22 04:09

Tilak