OpCodes.Initobj Field
This page is specific to:.NET Framework Version:1.12.03.03.5Silverlight 34.0
OpCodes.Initobj Field
Initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type.

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

Syntax

'Usage

Dim value As OpCode

value = OpCodes.Initobj


'Declaration

Public Shared ReadOnly Initobj As OpCode
public static final OpCode Initobj
Not applicable.
Remarks

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

FE 15 < T >

initobj typeTok

Initializes a value type.

The stack transitional behavior, in sequential order, is:

  1. The address of the value type to initialize is pushed onto the stack.

  2. The address is popped from the stack; the value type at the specified address is initialized as type typeTok.

The initobj instruction initializes each field of the value type specified by the pushed address (of type native int, &, or *) to a null reference or a 0 of the appropriate primitive type. After this method is called, the instance is ready for a constructor method to be called. If typeTok is a reference type, this instruction has the same effect as ldnull followed by stind.ref.

Unlike Newobj, initobj does not call the constructor method. Initobj is intended for initializing value types, while newobj is used to allocate and initialize objects.

The following Emit method overload can use the initobj opcode:

  • ILGenerator.Emit(OpCode, Type)

Platforms

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
See Also

Community Content

Initobj for reference types and generic parameters
Added by:Fabian Schmied

The MSDN docs on OpCodes.Initobj specify that "Behavior is unspecified if [...] classTok does not represent a value type."

This is not consistent with the ECMA specification, which says (ECMA-335, 4th edition/June 2006): "If typeTok is a reference type, the initobj instruction has the same effect as ldnull followed by stind.ref."

The C# compiler itself emits an initobj instruction for initialization of generic parameters with "default (T)", no matter whether it can guarantee T is a value type or not. Following this example, it seems to be safe to follow the ECMA specification and ignore the MSDN docs.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View