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.