This documentation is archived and is not being maintained.

MethodBody.InitLocals Property

Gets a value indicating whether local variables in the method body are initialized to the default values for their types.

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

public virtual bool InitLocals { get; }

Property Value

Type: System.Boolean
true if the method body contains code to initialize local variables to null for reference types, or to the zero-initialized value for value types; otherwise, false.

The InitLocals property refers to variables that are not explicitly initialized; that is, variables that are declared with syntax such as int x; in C# or Dim x As Integer in Visual Basic.

Reference variables are initialized to null by default. Numeric variables are initialized to zero.

The following code example defines a test method named MethodBodyExample and displays its local variable information and exception-handling clauses. The MethodBase.GetMethodBody method is used to obtain a MethodBody object for the test method. The MaxStackSize and InitLocals properties are displayed.

This code example is part of a larger example provided for the MethodBody class.

using System;
using System.Reflection;

public class Example
    public static void Main()
        // Get method body information.
        MethodInfo mi = typeof(Example).GetMethod("MethodBodyExample");
        MethodBody mb = mi.GetMethodBody();
        Console.WriteLine("\r\nMethod: {0}", mi);

        // Display the general information included in the 
        // MethodBody object.
        Console.WriteLine("    Local variables are initialized: {0}", 
        Console.WriteLine("    Maximum number of items on the operand stack: {0}", 



    // The Main method contains code to analyze this method, using
    // the properties and methods of the MethodBody class.
    public void MethodBodyExample(object arg)
        // Define some local variables. In addition to these variables,
        // the local variable list includes the variables scoped to 
        // the catch clauses.
        int var1 = 42;
        string var2 = "Forty-two";

            // Depending on the input value, throw an ArgumentException or 
            // an ArgumentNullException to test the Catch clauses.
            if (arg == null)
                throw new ArgumentNullException("The argument cannot be null.");
            if (arg.GetType() == typeof(string))
                throw new ArgumentException("The argument cannot be a string.");

        // There is no Filter clause in this code example. See the Visual 
        // Basic code for an example of a Filter clause.

        // This catch clause handles the ArgumentException class, and
        // any other class derived from Exception.
        catch(Exception ex)
            Console.WriteLine("Ordinary exception-handling clause caught: {0}", 
            var1 = 3033;
            var2 = "Another string.";

// This code example produces output similar to the following:
//Method: Void MethodBodyExample(System.Object)
//    Local variables are initialized: True
//    Maximum number of items on the operand stack: 2

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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