An object-creation-expression is used to create a new instance of a class-type or a value-type.
The type of an object-creation-expression must be a class-type or a value-type. The type cannot be an abstract
class-type.
The optional argument-list (§7.4.1) is permitted only if the type is a class-type or a struct-type.
The compile-time processing of an object-creation-expression of the form new
T(A)
, where T
is a class-type or a value-type and A
is an optional argument-list, consists of the following steps:
T
is a value-type and A
is not present:
T
, namely the default value for T
as defined in §4.1.1.T
is a class-type or a struct-type:
T
is an abstract
class-type, an error occurs.T
. If the set of candidate constructors is empty, or if a single best constructor cannot be identified, an error occurs.T
, namely the value produced by invoking the constructor determined in the step above.The run-time processing of an object-creation-expression of the form new
T(A)
, where T
is class-type or a struct-type and A
is an optional argument-list, consists of the following steps:
T
is a class-type:
T
is allocated. If there is not enough memory available to allocate the new instance, an OutOfMemoryException
is thrown and no further steps are executed.this
.T
is a struct-type:
T
is created by allocating a temporary local variable. Since a constructor of a struct-type is required to definitely assign a value to each field of the instance being created, no initialization of the temporary variable is necessary.this
.