ILGenerator.DeclareLocal Method (Type)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Declares a local variable of the specified type.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- localType
- Type: System.Type
The type of the local variable.
| Exception | Condition |
|---|---|
| ArgumentNullException | 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);