Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml


Definite assignment

C sharp



+ Font mai mare | - Font mai mic



Definite assignment

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by static flow analysis, that the variable has been automatically initialized or has been the target of at least one assignment. The rules of definite assignment are:



An initially assigned variable (5.3.1) is always considered definitely assigned.

An initially unassigned variable (5.3.2) is considered definitely assigned at a given location if all possible execution paths leading to that location contain at least one of the following:

A simple assignment (7.13.1) in which the variable is the left operand.

An invocation expression (7.5.5) or object creation expression (7.5.10.1) that passes the variable as an output parameter.

For a local variable, a local variable declaration (8.5) that includes a variable initializer.

The definite assignment states of instance variables of a struct-type variable are tracked individually as well as collectively. In additional to the rules above, the following rules apply to struct-type variables and their instance variables:

An instance variable is considered definitely assigned if its containing struct-type variable is considered definitely assigned.

A struct-type variable is considered definitely assigned if each of its instance variables are considered definitely assigned.

Definite assignment is a requirement in the following contexts:

A variable must be definitely assigned at each location where its value is obtained. This ensures that undefined values never occur. The occurrence of a variable in an expression is considered to obtain the value of the variable, except when

the variable is the left operand of a simple assignment,

the variable is passed as an output parameter, or

the variable is a struct-type variable and occurs as the left operand of a member access.

A variable must be definitely assigned at each location where it is passed as a reference parameter. This ensures that the function member being invoked can consider the reference parameter initially assigned.

All output parameters of a function member must be definitely assigned at each location where the function member returns (through a return statement or through execution reaching the end of the function member body). This ensures that function members do no return undefined values in output parameters, thus enabling the compiler to consider a function member invocation that takes a variable as an output parameter equivalent to an assignment to the variable.

The this variable of an instance constructor of a struct-type must be definitely assigned at each location where the constructor returns.

The following example demonstrates how the different blocks of a try statement (8.10) affect definite assignment.

class A

catch
finally
// i and j definitely assigned
}
}

The static flow analysis performed to determine the definite assignment state of a variable takes into account the special behavior of the &&, ||, and ?: operators. In each of the methods in the example

class A

else
// i not definitely assigned
}

static void G(int x, int y) {
int i;
if (x >= 0 || (i = y) >= 0)
else
// i not definitely assigned
}
}

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. In the if statement in the F method, the variable i is definitely assigned in the first embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. In contrast, the variable i is not definitely assigned in the second embedded statement since the x >= 0 might have tested false, resulting in variable i being unassigned. Similarly, in the G method, the variable i is definitely assigned in the second embedded statement but not in the first embedded statement.

Initially assigned variables

The following categories of variables are classified as initially assigned:

Static variables.

Instance variables of class instances.

Instance variables of initially assigned struct variables.

Array elements.

Value parameters.

Reference parameters.

Initially unassigned variables

The following categories of variables are classified as initially unassigned:

Instance variables of initially unassigned struct variables.

Output parameters, including the this variable of instance constructors for structs.

Local variables.

Variable references

A variable-reference is an expression that is classified as a variable. A variable-reference denotes a storage location that can be accessed both to fetch the current value and to store a new value. In C and C++, a variable-reference is known as an lvalue.

variable-reference:
expression

The following constructs require an expression to be a variable-reference:

The left hand side of an assignment (which may also be a property access or an indexer access).

An argument passed using ref or out in a method or instance constructor invocation.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 971
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved