Format | Assembly Format | Description |
---|---|---|
73 <T> | newobj ctor |
Allocate an uninitialized object or value type and call ctor. |
…, arg1, … argN → …, obj
The newobj
instruction creates a new object or a new instance of a value type. ctor is a metadata token (a methodref
or methodef
that shall be marked as a constructor; see Partition II) that indicates the name, class, and signature of the constructor to call. If a constructor exactly matching the indicated name, class and signature cannot be found, MissingMethodException
is thrown.
The newobj
instruction allocates a new instance of the class associated with ctor and initializes all the fields in the new instance to 0 (of the proper type) or null
as appropriate. It then calls the constructor with the given arguments along with the newly created instance. After the constructor has been called, the now initialized object reference is pushed on the stack.
From the constructor's point of view, the uninitialized object is argument 0 and the other arguments passed to newobj
follow in order.
All zero-based, one-dimensional arrays are created using newarr
, not newobj
. On the other hand, all other arrays (more than one dimension, or one-dimensional but not zero-based) are created using newobj
.
Value types are not usually created using newobj
. They are usually allocated either as arguments or local variables, using newarr
(for zero-based, one-dimensional arrays), or as fields of objects. Once allocated, they are initialized using initobj
. However, the newobj
instruction can be used to create a new instance of a value type on the stack, that can then be passed as an argument, stored in a local, etc.
System.InvalidOperationException
is thrown if ctor's class is abstract.
System.MethodAccessException
is thrown if ctor is inaccessible.
System.OutOfMemoryException
is thrown if there is insufficient memory to satisfy the request.
System.MissingMethodException
is thrown if a constructor method with the indicated name, class, and signature could not be found. This is typically detected when CIL is converted to native code, rather than at runtime.
Correct CIL ensures that ctor is a valid methodref
or methoddef
token, and that the arguments on the stack are assignable-to (§I.8.7.3) the parameters of the constructor.
Verification depends on whether a delegate or other object is being created. There are three cases, in order:
-
If the
newobj
instruction is part of adup
;ldvirtftn
;newobj
instruction sequence and the ctor metadata token references a delegate type then a delegate for a virtual function is being created; -
If the
newobj
instruction is part of aldftn
;newobj
instruction sequence and the ctor metadata token references a delegate type then a delegate for a static or non-virtual instance function is being created; -
Otherwise if the ctor metadata token does not references a delegate type then some other object is being created.
No other cases are verifiable. The different verification rules for the three cases follow.
When a newobj
instruction is part of a:
dup
ldvirtftn <function>
newobj <ctor>
instruction sequence then verification checks that:
-
there is a target on the stack prior to the
dup
instruction of type T; -
function is a
methoddef
,methodref
ormethodspec
metadata token for a virtual method on type T; -
ctor is a
methoddef
ormethodref
metadata token marked as a constructor for a delegate type deltype; -
ctor is accessible from the
newobj
site; -
the signature of function is delegate-assignable-to the signature of deltype (i.e. the signature of the
Invoke
method of deltype); -
the verification type of target is verifier-assignable-to (§III.1.8.1.2.3) the this signature of function; and
-
no branch instructions target the
ldvirtftn
ornewobj
instructions within the sequence.
Verification tracks the type of obj as deltype.
When a newobj
instruction is part of a:
ldftn <function>
newobj <ctor>
instruction sequence then verification checks that:
-
function is a
methoddef
,methodref
ormethodspec
metadata token for a static or nonvirtual instance method; -
there is a target on the stack prior to the
ldftn
instruction and the verification type of target is either:-
verifier-assignable-to (§III.1.8.1.2.3) the this signature of function, if function refers to an instance method, or
-
null (i.e. the result of
ldnull
), if function refers to a static method
-
-
ctor is a
methoddef
ormethodref
metadata token marked as a constructor for a delegate type deltype; -
ctor is accessible from the
newobj
site; -
the signature of function is delegate-assignable-to the signature of deltype (i.e. the signature of the
Invoke
method of deltype); and -
when function is a non-final virtual method and the target on the stack is not a boxed valued type, verification checks that target is the result of
ldarg.s 0
,ldarg 0
orldarg.0
and the creator's body does not containstarg.s 0
,starg 0
orldarga.s 0
,ldarga 0
. [Note: This mirrors the requirement, and rationale, for thecall
instruction (§III.3.19). end note]; and -
no branch instructions target the
newobj
instruction within the sequence.
Verification tracks the type of obj as deltype.
Verification checks that:
-
ctor is a
methoddef
ormethodref
metadata token marked as a constructor for a non-delegate type T; -
ctor is accessible from the
newobj
site; and -
the types of the arguments; arg1, … argN; on the stack are verifier-assignable-to (§III.1.8.1.2.3) the parameter signatures of ctor's signature.
Verification tracks the type of obj as T.