Export (0) Print
Expand All
Expand Minimize

OpCodes.Newobj Field

Creates a new object or a new instance of a value type, pushing an object reference (type O) onto the evaluation stack.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public static readonly OpCode Newobj

The following table lists the instruction's hexadecimal and Microsoft Intermediate Language (MSIL) assembly format, along with a brief reference summary:

Format

Assembly Format

Description

73 < T >

newobj ctor

Allocates an uninitialized object or value type and calls the constructor method ctor.

The stack transitional behavior, in sequential order, is:

  1. Arguments arg1 through argn are pushed on the stack in sequence.

  2. Arguments argn through arg1 are popped from the stack and passed to ctor for object creation.

  3. A reference to the new object is pushed onto the stack.

The newobj instruction creates a new object or a new instance of a value type. Ctor is a metadata token (a methodref or methoddef that must be marked as a constructor) that indicates the name, class and signature of the constructor to call.

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 references as appropriate. It then calls the constructor ctor with the given arguments along with the newly created instance. After the constructor has been called, the now initialized object reference (type O) 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, and so on.

OutOfMemoryException is thrown if there is insufficient memory to satisfy the request.

MissingMethodException is thrown if a constructor method ctor with the indicated name, class and signature could not be found. This is typically detected when Microsoft Intermediate Language (MSIL) instructions are converted to native code, rather than at runtime.

The following Emit method overload can use the newobj opcode:

  • ILGenerator.Emit(OpCode, ConstructorInfo)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft