インターフェイスにはコンストラクターがなく、それ自体で作成することはできません。
オブジェクトを変数 (インターフェイス タイプの変数を含む) に割り当てても、新しいオブジェクトは作成されず、同じオブジェクトへの別の参照にすぎません。
class DerivedWithInterface: Base, IEnumerable {}
これで、クラスのインスタンスを作成DerivedWithInterface
し、任意の基本クラス/インターフェイスの変数に割り当てることができますが、new
作成されるのはオブジェクトのみです。
DerivedWithInterface item = new DerivedWithInterface();
IEnumerable asEnumerable = item; // asEnumerable is the same object as create before
Base asBase = item;
これで、元のオブジェクトにキャストを戻すことができますが、正確に 1 つ (またはnew
編集した数だけ)が存在します。
IEnumerable asEnumerableItem = new DerivedWithInterface();
DerivedWithInterface itemViaCast = (DerivedWithInterface)asEnumerableItem;
とタイプの および オブジェクトの同じ単一のインスタンスを参照asEnumerableItem
するitemViaCast
asEnumerableItem