Interface Examples

I am new to C # .net and was surprised to learn that interface instances can be created as

Iinterface myDimensions = (Iinterface) myBox; 

How is memory allocated for this type of operator? Is memory allocated to the heap?

Can anyone give any situation when these types of instances are used.

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

Why is such a restriction applied in the language?

Thanks,

+6
c # interface
source share
8 answers

Before answering your first question, I note that you have two questions. In the future, consider two questions about stack overflows when you have two questions. As you may have noticed, when you ask two questions in one, often the second question is ignored by almost everyone.

I was surprised to learn that interface instances can be created as

 Iinterface myDimensions = (Iinterface) myBox; 

How is memory allocated for this type of operator? Is memory allocated to the heap?

As others have pointed out, it is not necessary to instantiate a type that implements the interface. The fact that everyone seems to have forgotten in their haste to tell you that reference conversions do not allocate memory is that box conversions allocate memory. If myBox has a structure type, this will allocate memory on the heap for the "wrapper" and make a copy of the value in the wrapper. The shell then implements the interface.

Turning to your second question:

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

The purpose of explicitly implementing an interface is to enable the class to implement a specific interface without requiring that these methods appear in places where they are not needed. For example, suppose you have:

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

If removing the open connection is the same as closing the connection, you probably don't want to confuse your users: (1) with two methods that do the same, or (2) using the OpenConnection method paired with an unobvious name, for example Dispose Allowing you to make Dispose โ€œinvisibleโ€ if the object is not converted to IDisposable will make it easier for your users to find the right action.

Another circumstance in which you will use this is when you have two interfaces with the same name:

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

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

+13
source share

This is not an instance - this is a type. An instance is a source object, i.e. myBox As for the memory, yes, there is allocated memory for the link - regardless of whether it is completely on the heap or the stack completely depends on the context. In your example (which looks like a function in a function), I would guess the stack.

As for the explicit implementation: the language function allows one class to implement two or more interfaces containing one or more members that have exactly the same signature. For example:

 interface A { void Foo(); } interface B { void Foo(); } class C : A, B { void A.Foo() { } void B.Foo() { } } 

Without this function, it is unclear to the compiler that implements the C.Foo interface C.Foo . Of course, the caveat is that callers cannot simply call C.Foo , since it would also not be obvious which method to call; therefore, an object of type C must first be ported to A or B in order to make the intent of the Foo-lish programmer understandable.

+10
source share

You have a throw, not an instance. In this case, the object should already be created somewhere else:

 // Allocate memory on the stack to point to the location on the heap // to store the object and create the object on the heap. Box box = new Box(); // Allocate memory on the stack to point to the location on the heap // and point it to the already existing object on the heap. IInterface iBox = (IInterface)box; 

A quote from MSDN is provided in the "Explicit Interface Implementations" section. This can be a bit confusing. The simplest example:

 public interface ISomething { void SayHi(); } public class Something : ISomething { public void SayHi() { Console.WriteLine("Hello World!"); } public void ISomething.SayHi() { Console.WriteLine("42!"); } } 

Now your code may have something like the following:

 Something obj = new Something(); // Outputs "Hello World!" obj.SayHi(); ISomething iObj = obj; // Outputs "42!" iObj.SayHi(); 

In this example, the memory works the same way as I explained earlier.

+7
source share

your expression

 Iinterface myDimensions = (Iinterface) myBox; 

It does not create anything, it robs the myBox object (which must be created somewhere) for the Iinterface type and points myDimesions to this object.

+2
source share

The language in the MS # C # Link for interface bit misleading. When it says an instance of an interface, it actually means a link to the interface, which you can get by hovering an object (i.e. an instance of the class) on the type of interface.

Interface members can be implicitly implemented or explicitly implemented. When it is explicitly implemented, the interface name is used to determine it:

 public class Box: IInterface { void IInterface.Foo() { ... } } 

This hides the element from the open class interface. One reason for this is for version control, for example, when a class already has an unrelated Foo() method. To specify IInterface.Foo() rather than Box.Foo() , the only way is through a link to IInterface , either by casting it, or through a variable declared as IInterface . You can just as easily:

  IInterface myDimensions = myBox; //no casting needed myDimensions.Foo(); //calls Box.IInterface.Foo() and NOT Box.Foo() 
+2
source share

Declaration:

 interface IMy { void OhMy(); } class Explicit : IMy { public void IMy.OhMy() { } } class Implicit : IMy { public void OhMy() { } } 

Using:

 Implicit i = new Implicit(); i.OhMy(); // ok Explicit e = new Explicit(); e.OhMy(); // it cannot be accessed through a class instance ((IMy)e).OhMy(); // but only through an instance of the interface IMy my = new Explicit(); my.OhMy(); // ok 
+2
source share

Basically, you have an X object that the variable name myBox points to. The memory allocation of an object depends on how you create this X object.

Code: Iinterface myDimensions = (Iinterface) myBox; just assign your X object to the variable "myDimensions". Since this variable is an Iinterface, you must use (Iinterface) to create it. This is a type safety feature that lets you know which object you are facing.

You now have at least two variables, myBox and myDimensions, that point to an X object.

You can create as many variables as you want to point to this object. You will need to point "myBox" to "myDimensions" at the time of assignment if the variable type myBox does not implement "Iinterface", although your X object supports Iinterface.

As a side note, variables also take up memory because it needs to store the address of the actual instance

0
source share

Can anyone give any situation when these types of instances are used.

As others have said, this is not really an instantiation. Regarding its use, one example is one that you actually won't see, because it happens under the covers. Consider the using statement

 using (DisposableObject foo = new DisposableObject()) { // doing things with foo } 

And how the compiler evaluates this expression and converts it into something like

 DisposableObject foo = null; try { foo = new DisposableObject(); // doing things with foo } finally { if (foo != null) { ((IDisposable)foo).Dispose(); } } 

Or consider other scenarios in which a method accepts an interface type rather than a class. This is due to the fact that you may have several classes that implement such an interface, but the method only concerns the fact that the objects of these classes fulfill the interface contract.

 public void Dance(ICanDance dancer, bool wantTo) { if (wantTo) dancer.Dance(); } 

The actual dancer can be anything that implements the interface, be it LipSynchingPopSinger , StageDancer or GuyThatIsNotTheFather .

0
source share

All Articles