Reputation: 3735
In C#, we often use a initializing syntax that a instance of a base class is initializing with its inherited child class like this:
Control BTN = new Button();
Now my question is just that, what is the formal name of this kind of initializing?
Upvotes: 2
Views: 748
Reputation: 838716
According to the C# specification, it is called simple assignment with an implicit reference conversion.
7.16.1 Simple assignment
In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand.
...
The run-time processing of a simple assignment of the form x = y consists of the following steps:
- ...
- y is evaluated and, if required, converted to the type of x through an implicit conversion (§6.1).
6.1.6 Implicit reference conversions
The implicit reference conversions are:
- ...
- From any class-type S to any class-type T, provided S is derived from T.
Upvotes: 6
Reputation: 2901
The most concise term I can think of is polymorphic instantiation, or polymorphic initialization. That seems to capture the necessary elements without writing a whole sentence. Not aware of a formal term, though.
Upvotes: 3
Reputation: 236268
This is not base class instance initializing. This is the inherited class instance initializing and referencing this instance via variable of base class type. You can call it assignment value to variable
. No special name.
Upvotes: 0