CATEGORII DOCUMENTE |
C# defines seven categories of variables: static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables. The sections that follow describe each of these categories.
In the example
class A
}
x is a static variable, y is an instance variable, v[0] is an array element, a is a value parameter, b is a reference parameter, c is an output parameter, and i is a local variable.
A field declared with the static modifier is called a static variable. A static variable comes into existence when the type in which it is declared is loaded (10.11), and ceases to exist when the program terminates.
The initial value of a static variable is the default value (5.2) of the variable's type.
For the purpose of definite assignment checking, a static variable is considered initially assigned.
A field declared without the static modifier is called an instance variable.
An instance variable of a class comes into existence when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instance's destructor (if any) has executed.
The initial value of an instance variable of a class is the default value (5.2) of the variable's type.
For the purpose of definite assignment checking, an instance variable of a class is considered initially assigned.
An instance variable of a struct has exactly the same lifetime as the struct variable to which it belongs. In other words, when a variable of a struct type comes into existence or ceases to exist, so too do the instance variables of the struct.
The initial assignment state of an instance variable of a struct is the same as that of the containing struct variable. In other words, when a struct variable is considered initially assigned, so too are its instance variables, and when a struct variable is considered initially unassigned, its instance variables are likewise unassigned.
The elements of an array come into existence when an array instance is created, and cease to exist when there are no references to that array instance.
The initial value of each of the elements of an array is the default value (5.2) of the type of the array elements.
For the purpose of definite assignment checking, an array element is considered initially assigned.
A parameter declared without a ref or out modifier is a value parameter.
A value parameter comes into existence upon invocation of the function member (7.4) 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 the purpose of definite assignment checking, a value parameter is considered initially assigned.
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).
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.
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 an instance constructor of a struct type, the this keyword behaves exactly as an output parameter of the struct type (7.5.7).
A local variable is declared by a local-variable-declaration, which may occur in a block, a for-statement, a switch-statement, or a using-statement. A local variable comes into existence when control enters the block, for-statement, switch-statement, or using-statement that immediately contains the local variable declaration. A local variable ceases to exist when control leaves its immediately containing block, for-statement, switch-statement, or using-statement.A local variable is not automatically initialized and thus has no default value. For the purpose 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.
The phrases "comes into existence" and "ceases to exist", as used above, do not refer to implementation. The actual lifetime of a variable is implementation-dependent. For example, a compiler might statically determine that a local variable in a block is only used for a small portion of the block, and use this analysis to generate more efficient code which causes the storage for the variable to have a shorter lifetime than its containing block.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 859
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved