[ Pobierz całość w formacie PDF ]
or operator) to which the parameter belongs, and is initialized with the value of the argument given in the
invocation. A value parameter ceases to exist upon return of the function member.
For purposes of definite assignment checking, a value parameter is considered initially assigned.
5.1.5 Reference parameters
A parameter declared with a ref modifier is a reference parameter.
A reference parameter does not create a new storage location. Instead, a reference parameter represents the same
storage location as the variable given as the argument in the function member invocation. Thus, the value of a
reference parameter is always the same as the underlying variable.
The following definite assignment rules apply to reference parameters. Note the different rules for output
parameters described in 5.1.6.
" A variable must be definitely assigned (5.3) before it can be passed as a reference parameter in a function
member invocation.
" Within a function member, a reference parameter is considered initially assigned.
Within an instance method or instance accessor of a struct type, the this keyword behaves exactly as a
reference parameter of the struct type (7.5.7).
5.1.6 Output parameters
A parameter declared with an out modifier is an output parameter.
An output parameter does not create a new storage location. Instead, an output parameter represents the same
storage location as the variable given as the argument in the function member invocation. Thus, the value of an
output parameter is always the same as the underlying variable.
The following definite assignment rules apply to output parameters. Note the different rules for reference
parameters described in 5.1.5.
68 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 5 Variables
" A variable need not be definitely assigned before it can be passed as an output parameter in a function
member invocation.
" Following a function member invocation, each variable that was passed as an output parameter is considered
assigned in that execution path.
" Within a function member, an output parameter is considered initially unassigned.
" Every output parameter of a function member must be definitely assigned (5.3) before the function
member returns.
Within a constructor of a struct type, the this keyword behaves exactly as an output parameter of the struct
type (7.5.7).
5.1.7 Local variables
A local variable is declared by a local-variable-declaration, which may occur in a block, a for-statement, or a
switch-statement. A local variable comes into existence when control enters the block, for-statement, or switch-
statement that immediately contains the local variable declaration. A local variable ceases to exist when control
leaves its immediately containing block , for-statement, or switch-statement.
A local variable is not automatically initialized and thus has no default value. For purposes of definite
assignment checking, a local variable is considered initially unassigned. A local-variable-declaration may
include a variable-initializer, in which case the variable is considered definitely assigned in its entire scope,
except within the expression provided in the variable-initializer.
Within the scope of a local variable, it is an error to refer to the local variable in a textual position that precedes
its variable-declarator.
5.2 Default values
The following categories of variables are automatically initialized to their default values:
" Static variables.
" Instance variables of class instances.
" Array elements.
The default value of a variable depends on the type of the variable and is determined as follows:
" For a variable of a value-type, the default value is the same as the value computed by the value-type s
default constructor (4.1.1).
" For a variable of a reference-type, the default value is null.
5.3 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.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 69
C# LANGUAGE REFERENCE
" 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 state 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
[ Pobierz całość w formacie PDF ]