Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface instances

Tags:

c#

.net

interface

I am new to C#.net and was surprised to know that instances of an Interface can be created like

Iinterface myDimensions = (Iinterface) myBox;

How is memory allocated for this type of statement? Is the memory allocated on heap?

Can any one give any situation where this types of instantiations are used.

A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.

Why is such a constraint enforced in the language?

Thanks,

like image 617
Sandeep Avatar asked Sep 16 '10 18:09

Sandeep


1 Answers

Before answering your first question, I note that you have two questions here. In the future, consider asking two separate questions on Stack Overflow when you have two questions. As you might have noticed, when you ask two questions in one, often the second question gets ignored by almost everyone.

I was surprised to know that instances of an interface can be created like

Iinterface myDimensions = (Iinterface) myBox; 

How is memory allocated for this type of statement? Is the memory allocated on heap?

As others have pointed out, this is not necessarily the creation of an instance of a type that implements the interface. What everyone seems to have forgotten in their haste to tell you that reference conversions do not allocate memory, is that boxing conversions do allocate memory. If myBox is of a struct type then this will allocate memory on the heap for a "wrapper" and make a copy of the value in the wrapper. The wrapper then implements the interface.

Moving on to your second question:

A class that implements an interface can explicitly implement a member of that interface. When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface. Why is such a constraint enforced in the language?

The purpose of explicit interface implementation is to enable a class to implement a particular interface without requiring that those methods show up in places they are not wanted. For example, suppose you have:

class MyConnection : IDisposable
{
    public void OpenConnnection() { ... }
    public void CloseConnection() { ... }
    public void Dispose() { ... CloseConnection(); ... }
}

If disposing an open connection is the same as closing a connection then you probably do not want to confuse your users by either (1) having two methods that do the same thing, or (2) having a method OpenConnection paired with a non-obvious name like Dispose. By enabling you to make Dispose "invisible" unless the object is converted to IDisposable then you make it easier on your users to discover the right thing to do.

Another circumstance where you'd use this is when you have two interfaces with the same name:

interface IFoo { void M(); }
interface IBar { void M(); }

Now how do you make a class C that implements both IFoo and IBar, but has different implementations for the two M methods? You have to use explicit implementation for one or both of them if you want two different bodies.

like image 186
Eric Lippert Avatar answered Oct 04 '22 04:10

Eric Lippert