ILGenerator.DeclareLocal Method (Type)

May 11, 2014

Declares a local variable of the specified type.

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

public virtual LocalBuilder DeclareLocal(
	Type localType


Type: System.Type
The type of the local variable.

Return Value

Type: System.Reflection.Emit.LocalBuilder
The declared local variable.


localType is null.

The local variable is created in the current lexical scope. For example, if code is being emitted in a for loop (For loop in Visual Basic), the scope of the variable is the loop.

A local variable that is created with this overload is not pinned. To create a pinned variable for use with unmanaged pointers, use the DeclareLocal(Type, Boolean) method overload.

The following example demonstrates the use of the DeclareLocal method to declare two local variables. This code is part of a larger example provided for the BeginExceptionBlock method.

// The exception that is thrown by DoAdd.
Type overflowType = typeof(OverflowException);

// Declare local variables to hold the result of the addition, and the
// exception that is caught by the Catch block.
LocalBuilder result = adderIL.DeclareLocal(typeof(int));
LocalBuilder thrownException = adderIL.DeclareLocal(overflowType);

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

© 2014 Microsoft