Table of Contents

Class ILVariable

Namespace
ICSharpCode.Decompiler.IL
Assembly
ICSharpCode.Decompiler.dll
public class ILVariable
Inheritance
ILVariable
Inherited Members

Constructors

ILVariable(VariableKind, IType, StackType, int?)

public ILVariable(VariableKind kind, IType type, StackType stackType, int? index = null)

Parameters

kind VariableKind
type IType
stackType StackType
index int?

ILVariable(VariableKind, IType, int?)

public ILVariable(VariableKind kind, IType type, int? index = null)

Parameters

kind VariableKind
type IType
index int?

Fields

StackType

public readonly StackType StackType

Field Value

StackType

StateMachineField

The field which was converted to a local variable. Set when the variable is from a 'yield return' or 'async' state machine.

public IField? StateMachineField

Field Value

IField

Properties

AddressCount

Number of ldloca instructions referencing this variable.

public int AddressCount { get; }

Property Value

int

Remarks

This variable is automatically updated when adding/removing ldloca instructions from the ILAst.

AddressInstructions

List of ldloca instructions referencing this variable.

public IReadOnlyList<LdLoca> AddressInstructions { get; }

Property Value

IReadOnlyList<LdLoca>

Remarks

This list is automatically updated when adding/removing ldloca instructions from the ILAst.

CaptureScope

Gets the block container in which this variable is captured. For captured variables declared inside the loop, the capture scope is the BlockContainer of the loop. For captured variables declared outside of the loop, the capture scope is the BlockContainer of the parent function.

public BlockContainer? CaptureScope { get; }

Property Value

BlockContainer

Remarks

This property returns null for variables that are not captured.

Function

Gets the function in which this variable is declared.

public ILFunction? Function { get; }

Property Value

ILFunction

Remarks

This property is set automatically when the variable is added to the ILFunction.Variables collection.

HasGeneratedName

public bool HasGeneratedName { get; set; }

Property Value

bool

HasInitialValue

[Obsolete("Use 'UsesInitialValue' instead.")]
public bool HasInitialValue { get; set; }

Property Value

bool

HasNullCheck

Gets/sets whether a parameter has an auto-generated null check, i.e., the !! modifier. Returns false for all variables except parameters.

public bool HasNullCheck { get; set; }

Property Value

bool

Index

The index of the local variable or parameter (depending on Kind)

For VariableKinds with "Local" in the name:

  • if non-null, the Index refers to the LocalVariableSignature.
  • index may be null for variables that used to be fields (captured by lambda/async) For Parameters, the Index refers to the method's list of parameters. The special "this" parameter has index -1. For ExceptionStackSlot, the index is the IL offset of the exception handler. For other kinds, the index has no meaning, and is usually null.
public int? Index { get; }

Property Value

int?

IndexInFunction

Gets the index of this variable within the Function.Variables collection.

public int IndexInFunction { get; }

Property Value

int

Remarks

This property is set automatically when the variable is added to the VariableScope.Variables collection. It may change if an item with a lower index is removed from the collection.

InitialValueIsInitialized

Gets/Sets whether the variable's initial value is initialized. This is always true for parameters (incl. this).

Normal variables have an initial value if the function uses ".locals init".

public bool InitialValueIsInitialized { get; set; }

Property Value

bool

IsDead

Gets whether the variable is dead - unused.

public bool IsDead { get; }

Property Value

bool

IsRefReadOnly

This variable is either a C# 7 'in' parameter or must be declared as 'ref readonly'.

public bool IsRefReadOnly { get; }

Property Value

bool

IsSingleDefinition

Gets whether the variable is in SSA form: There is exactly 1 store, and every load sees the value from that store.

public bool IsSingleDefinition { get; }

Property Value

bool

Remarks

Note: the single store is not necessary a store instruction, it might also be the use of the implicit initial value. For example: for parameters, IsSingleDefinition will only return true if the parameter is never assigned to within the function.

Kind

public VariableKind Kind { get; }

Property Value

VariableKind

LoadCount

Number of ldloc instructions referencing this variable.

public int LoadCount { get; }

Property Value

int

Remarks

This variable is automatically updated when adding/removing ldloc instructions from the ILAst.

LoadInstructions

List of ldloc instructions referencing this variable.

public IReadOnlyList<LdLoc> LoadInstructions { get; }

Property Value

IReadOnlyList<LdLoc>

Remarks

This list is automatically updated when adding/removing ldloc instructions from the ILAst.

Name

public string? Name { get; set; }

Property Value

string

StoreCount

Number of store instructions referencing this variable, plus 1 if HasInitialValue.

Stores are:

public int StoreCount { get; }

Property Value

int

Remarks

This variable is automatically updated when adding/removing stores instructions from the ILAst.

StoreInstructions

List of store instructions referencing this variable.

Stores are:

public IReadOnlyList<IStoreInstruction> StoreInstructions { get; }

Property Value

IReadOnlyList<IStoreInstruction>

Remarks

This list is automatically updated when adding/removing stores instructions from the ILAst.

Type

public IType Type { get; }

Property Value

IType

UsesInitialValue

Gets/Sets whether the initial value of the variable is used. This is always true for parameters (incl. this).

Normal variables use the initial value, if no explicit initialization is done.

public bool UsesInitialValue { get; set; }

Property Value

bool

Remarks

The following table shows the relationship between InitialValueIsInitialized and UsesInitialValue.

InitialValueIsInitializedUsesInitialValueMeaning
truetrueThis variable's initial value is zero-initialized (.locals init) and the initial value is used. From C#'s point of view a the value default(T) is assigned at the site of declaration.
truefalseThis variable's initial value is zero-initialized (.locals init) and the initial value is not used. From C#'s point of view no implicit initialization occurs, because the code assigns a value explicitly, before the variable is first read.
falsetrueThis variable's initial value is uninitialized (.locals without init) and the initial value is used. From C#'s point of view a call to
System.Runtime.CompilerServices.Unsafe.SkipInit(out T)
is generated after the declaration.
falsefalseThis variable's initial value is uninitialized (.locals without init) and the initial value is not used. From C#'s point of view no implicit initialization occurs, because the code assigns a value explicitly, before the variable is first read.

Methods

ToString()

public override string? ToString()

Returns

string