ILGenerator.DeclareLocal Method (Type)

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 containing type has been created by the TypeBuilder.CreateType method.

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);


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions