As far as I know, interfaces cannot be instantiated.
If this is true, why does the below code compile and execute? It allows you to create a variable interface. Why is this possible?
Interface:
public interface IDynamicCode<out TCodeOut>
{
object DynamicClassInstance { get; set; }
TCodeOut Execute(string value = "");
}
InCode:
var x = new IDynamicCode<string>[10];
Result:
UPDATE:
It only happens when array declared. Not a single instance.
You're not instantiating an interface, but an array of that interface.
You can assign an instance of any class that implements
IDynamicCode<string>
to that array. Say you havepublic class Foo : IDynamicCode<string> { }
, you can instantiate that and assign it to an element of that array:Instantiating the interface would not compile: