# 變數Variables

## 變數類別目錄Variable categories

C#定義變數的七個類別：靜態變數、執行個體變數、陣列元素、值參數、參考參數、輸出參數和本機變數。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.

class A
{
public static int x;
int y;

void F(int[] v, int a, ref int b, out int c) {
int i = 1;
c = a + b++;
}
}


x是一個靜態變數， y它是一個執行個體變數v[0] ，是一個陣列元素a ，是一個c值參數b ，是一個參考參數，是一個輸出參數， i而是一個本機變數.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.

### 靜態變數Static variables

static修飾詞宣告的欄位稱為靜態變數A field declared with the static modifier is called a static variable. 靜態變數在執行其包含類型的靜態函式（靜態的函式）之前就會存在，而且當相關聯的應用程式域停止存在時，就會停止存在。A static variable comes into existence before execution of the static constructor (Static constructors) for its containing type, and ceases to exist when the associated application domain ceases to exist.

### 傳址參數Reference parameters

• 變數必須是明確指派的（明確指派），才能在函式成員或委派調用中傳遞做為參考參數。A variable must be definitely assigned (Definite assignment) before it can be passed as a reference parameter in a function member or delegate invocation.
• 在函式成員或匿名函式內，會將參考參數視為一開始指派。Within a function member or anonymous function, a reference parameter is considered initially assigned.

### 輸出參數Output parameters

• 變數不需要明確指派，就可以在函式成員或委派調用中傳遞做為輸出參數。A variable need not be definitely assigned before it can be passed as an output parameter in a function member or delegate invocation.
• 在函式成員或委派調用的正常完成之後，每個當做輸出參數傳遞的變數都會視為該執行路徑中的指派。Following the normal completion of a function member or delegate invocation, each variable that was passed as an output parameter is considered assigned in that execution path.
• 在函數成員或匿名函式內，會將輸出參數視為一開始未指派。Within a function member or anonymous function, an output parameter is considered initially unassigned.
• 函式成員或匿名函式的每個輸出參數都必須明確指派（明確指派），函式成員或匿名函式才會正常傳回。Every output parameter of a function member or anonymous function must be definitely assigned (Definite assignment) before the function member or anonymous function returns normally.

### 區域變數Local variables

Local_variable_declaration引進的區域變數不會自動初始化，因此沒有預設值。A local variable introduced by a local_variable_declaration is not automatically initialized and thus has no default value. 基於明確指派檢查的目的， local_variable_declaration引進的本機變數會被視為一開始未指派。For the purpose of definite assignment checking, a local variable introduced by a local_variable_declaration is considered initially unassigned. Local_variable_declaration可能包含local_variable_initializer，在此情況下，只會將變數視為明確指派給初始化運算式（宣告語句）。A local_variable_declaration may include a local_variable_initializer, in which case the variable is considered definitely assigned only after the initializing expression (Declaration statements).

local_variable_declaration所引進的本機變數範圍內，在其local_variable_declarator之前的文字位置中參考該區域變數是編譯時期錯誤。Within the scope of a local variable introduced by a local_variable_declaration, it is a compile-time error to refer to that local variable in a textual position that precedes its local_variable_declarator. 如果區域變數宣告是隱含的（區域變數宣告），則在其local_variable_declarator中參考該變數也會是錯誤。If the local variable declaration is implicit (Local variable declarations), it is also an error to refer to the variable within its local_variable_declarator.

Foreach_statementspecific_catch_clause所引進的本機變數會被視為在其整個範圍中明確指派。A local variable introduced by a foreach_statement or a specific_catch_clause is considered definitely assigned in its entire scope.

## 預設值Default values

• 靜態變數。Static variables.
• 類別實例的執行個體變數。Instance variables of class instances.
• 陣列元素。Array elements.

• 對於value_type的變數，預設值與value_type的預設函式（預設的函式）所計算的值相同。For a variable of a value_type, the default value is the same as the value computed by the value_type's default constructor (Default constructors).
• 若為reference_type的變數，預設值為nullFor a variable of a reference_type, the default value is null.

## 明確指派Definite assignment

• 如果將執行個體變數的包含struct_type變數視為明確指派，則會將它視為明確指派。An instance variable is considered definitely assigned if its containing struct_type variable is considered definitely assigned.
• 如果將每個執行個體變數視為明確指派，則會將struct_type變數視為明確指派。A struct_type variable is considered definitely assigned if each of its instance variables is considered definitely assigned.

• 變數必須在取得其值的每個位置明確指派。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
• 變數是struct_type變數，並會當做成員存取的左運算元。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.
• 函式成員的所有輸出參數都必須在函式成員傳回的每個位置（透過return語句或透過執行到達函式成員主體的結尾）明確指派。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 not 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.
• Struct_type 實例的函式的變數必須在該實例的函式傳回的每個位置明確指派。thisThe this variable of a struct_type instance constructor must be definitely assigned at each location where that instance constructor returns.

### 初始指派的變數Initially assigned variables

• 靜態變數。Static variables.
• 類別實例的執行個體變數。Instance variables of class instances.
• 最初指派之結構變數的執行個體變數。Instance variables of initially assigned struct variables.
• 陣列元素。Array elements.
• 值參數。Value parameters.
• 參考參數。Reference parameters.
• catch 子句foreach或語句中宣告的變數。Variables declared in a catch clause or a foreach statement.

### 一開始未指派的變數Initially unassigned variables

• 一開始未指派之結構變數的執行個體變數。Instance variables of initially unassigned struct variables.
• 輸出參數，包括this結構實例函式的變數。Output parameters, including the this variable of struct instance constructors.
• 區域變數，但catch子句foreach或語句中所宣告的變數除外。Local variables, except those declared in a catch clause or a foreach statement.

### 判斷明確指派的精確規則Precise rules for determining definite assignment

• 在每個語句的開頭At the beginning of each statement
• 每個語句的結尾點（結束點和可連線）At the end point (End points and reachability) of each statement
• 在將控制項傳輸至另一個語句或語句結束點的每個 arc 上On each arc which transfers control to another statement or to the end point of a statement
• 在每個運算式的開頭At the beginning of each expression
• 在每個運算式的結尾處At the end of each expression

V的明確指派狀態可以是：The definite assignment state of v can be either:

• 明確指派。Definitely assigned. 這表示在此點的所有可能控制流程上，已將值指派給vThis indicates that on all possible control flows to this point, v has been assigned a value.
• 未明確指派。Not definitely assigned. 針對類型bool之運算式結尾的變數狀態，未明確指派之變數的狀態可能會屬於下列子狀態之一（但不一定）：For the state of a variable at the end of an expression of type bool, the state of a variable that isn't definitely assigned may (but doesn't necessarily) fall into one of the following sub-states:
• 在 true 運算式之後明確指派。Definitely assigned after true expression. 此狀態表示如果布林運算式評估為 true，則會明確指派v ，但如果布林運算式評估為 false，則不一定會指派。This state indicates that v is definitely assigned if the boolean expression evaluated as true, but is not necessarily assigned if the boolean expression evaluated as false.
• 在 false 運算式之後明確指派。Definitely assigned after false expression. 此狀態表示如果布林運算式評估為 false，則會明確指派v ，但如果布林運算式評估為 true，則不一定會指派。This state indicates that v is definitely assigned if the boolean expression evaluated as false, but is not necessarily assigned if the boolean expression evaluated as true.

#### 語句的一般規則General rules for statements

• v不會在函式成員主體的開頭明確指派。v is not definitely assigned at the beginning of a function member body.
• v會在任何無法連線的語句開頭明確指派。v is definitely assigned at the beginning of any unreachable statement.
• 在任何其他語句開頭的v明確指派狀態是藉由檢查所有目標為該語句開頭之控制流程傳輸上v的明確指派狀態來決定。The definite assignment state of v at the beginning of any other statement is determined by checking the definite assignment state of v on all control flow transfers that target the beginning of that statement. 如果（而且只有在） v是在所有此類控制流程傳輸上明確指派，則會在語句的開頭明確指派vIf (and only if) v is definitely assigned on all such control flow transfers, then v is definitely assigned at the beginning of the statement. 一組可能的控制流程傳輸的判斷方式，與檢查語句可連線性（端點和可連線性）相同。The set of possible control flow transfers is determined in the same way as for checking statement reachability (End points and reachability).
• Vchecked區塊、 、、lockif 、、、、do、或的結束點的明確指派狀態。 while unchecked for foreach using switch語句的決定方式是在所有以該語句的結束點為目標的控制流程傳輸上，檢查v的明確指派狀態。The definite assignment state of v at the end point of a block, checked, unchecked, if, while, do, for, foreach, lock, using, or switch statement is determined by checking the definite assignment state of v on all control flow transfers that target the end point of that statement. 如果在所有這類的控制流程傳輸上都已明確指派v ，則會在語句的結束點明確指派vIf v is definitely assigned on all such control flow transfers, then v is definitely assigned at the end point of the statement. 別的v不會在語句的結束點明確指派。Otherwise; v is not definitely assigned at the end point of the statement. 一組可能的控制流程傳輸的判斷方式，與檢查語句可連線性（端點和可連線性）相同。The set of possible control flow transfers is determined in the same way as for checking statement reachability (End points and reachability).

#### 運算式陳述式Expression statements

• vexpr開頭具有相同的明確指派狀態，如同在stmt開頭。v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
• 如果v是在expr結尾明確指派，則會在stmt的結束點明確指派它;別的它不會在stmt的結束點明確指派。If v if definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; otherwise; it is not definitely assigned at the end point of stmt.

#### 宣告陳述式Declaration statements

• 如果stmt不是沒有初始化運算式的宣告語句，則v 在 stmt 的結束點具有相同的明確指派狀態，如同在stmt的開頭。If stmt is a declaration statement without initializers, then v has the same definite assignment state at the end point of stmt as at the beginning of stmt.
• 如果stmt是具有初始化運算式的宣告語句，則v的明確指派狀態會決定為語句清單，每個具有初始化運算式的宣告都有一個指派語句（順序為宣告）。If stmt is a declaration statement with initializers, then the definite assignment state for v is determined as if stmt were a statement list, with one assignment statement for each declaration with an initializer (in the order of declaration).

#### If 語句If statements

if ( expr ) then_stmt else else_stmt

• vexpr開頭具有相同的明確指派狀態，如同在stmt開頭。v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
• 如果在expr的結尾明確指派v ，則在控制流程傳輸至then_stmt ，以及如果沒有 else子句時， 會將它明確指派給else_stmt或指標的端點。If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to then_stmt and to either else_stmt or to the end-point of stmt if there is no else clause.
• 如果vexpr結尾處的「true expression 之後明確指派」狀態，則會在控制流程傳輸至then_stmt時明確指派，而不會在控制流程傳輸上明確指派給任一else_stmt ，或如果沒有 else 子句，則為stmt的結束點。If v has the state "definitely assigned after true expression" at the end of expr, then it is definitely assigned on the control flow transfer to then_stmt, and not definitely assigned on the control flow transfer to either else_stmt or to the end-point of stmt if there is no else clause.
• 如果vexpr結尾處具有「在 false 運算式之後明確指派」狀態，則會在控制流程傳送至else_stmt時明確指派，而不會在控制流程傳輸至 then_stmt 時明確指派。 .If v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to else_stmt, and not definitely assigned on the control flow transfer to then_stmt. 只有在then_stmt的端點明確指派時，才會在stmt的端點上明確指派。It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the end-point of then_stmt.
• 否則 ，如果沒有else 子句，則會將v視為不會在控制流程傳輸到then_stmtelse_stmt時明確指派。Otherwise, v is considered not definitely assigned on the control flow transfer to either the then_stmt or else_stmt, or to the end-point of stmt if there is no else clause.

#### Switch 語句Switch statements

• Vexpr開頭的明確指派狀態，與stmt開頭的v狀態相同。The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
• 在控制流程上的v明確指派狀態傳送至可連線的 switch block 語句清單，與expr結尾的v明確指派狀態相同。The definite assignment state of v on the control flow transfer to a reachable switch block statement list is the same as the definite assignment state of v at the end of expr.

#### While 語句While statements

while ( expr ) while_body

• vexpr開頭具有相同的明確指派狀態，如同在stmt開頭。v has the same definite assignment state at the beginning of expr as at the beginning of stmt.
• 如果在expr結尾明確指派v ，則會在控制流程傳輸上明確指派給while_bodystmt的結束點。If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to while_body and to the end point of stmt.
• 如果vexpr結尾處的「true expression 之後明確指派」狀態，則會在控制流程傳送至while_body時明確指派，但不會在stmt的端點上明確指派。If v has the state "definitely assigned after true expression" at the end of expr, then it is definitely assigned on the control flow transfer to while_body, but not definitely assigned at the end-point of stmt.
• 如果vexpr結尾處具有「在 false 運算式之後明確指派」狀態，則會在控制流程傳輸到stmt的結束點時明確指派，但不會在控制流程傳輸上明確指派 _bodyIf v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control flow transfer to while_body.

#### Do 語句Do statements

do do_body while ( expr ) ;

• v在控制流程上具有相同的明確指派狀態，從stmt開頭到do_body ，如同在stmt的開頭。v has the same definite assignment state on the control flow transfer from the beginning of stmt to do_body as at the beginning of stmt.
• vexpr的開頭具有相同的明確指派狀態，如同do_body的結束點。v has the same definite assignment state at the beginning of expr as at the end point of do_body.
• 如果在expr的結尾明確指派v ，則會在控制流程傳輸上，將它明確指派給stmt的結束點。If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.
• 如果vexpr結尾處具有「在 false 運算式之後明確指派」狀態，則會在控制流程傳輸上，將它明確指派給stmt的結束點。If v has the state "definitely assigned after false expression" at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.

#### 適用于語句For statements

for語句的明確指派檢查，其格式如下：Definite assignment checking for a for statement of the form:

for ( for_initializer ; for_condition ; for_iterator ) embedded_statement


{
for_initializer ;
while ( for_condition ) {
embedded_statement ;
for_iterator ;
}
}


#### Break、continue 和 goto 語句Break, continue, and goto statements

break在、或continue 語句goto所造成的控制流程傳輸上，v 的明確指派狀態，與語句開頭的v明確指派狀態相同。The definite assignment state of v on the control flow transfer caused by a break, continue, or goto statement is the same as the definite assignment state of v at the beginning of the statement.

#### Throw 語句Throw statements

throw expr ;


Vexpr開頭的明確指派狀態，與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at the beginning of stmt.

#### Return 語句Return statements

return expr ;

• Vexpr開頭的明確指派狀態，與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at the beginning of stmt.
• 如果v是輸出參數，則必須明確地指派其中一個：If v is an output parameter, then it must be definitely assigned either:
• after expr之後after expr
• 或，位於括finallytry語句- try之或finally 的區塊-結尾。 catch - finally returnor at the end of the finally block of a try-finally or try-catch-finally that encloses the return statement.

return ;

• 如果v是輸出參數，則必須明確地指派其中一個：If v is an output parameter, then it must be definitely assigned either:
• stmt之前before stmt
• 或，位於括finallytry語句- try之或finally 的區塊-結尾。 catch - finally returnor at the end of the finally block of a try-finally or try-catch-finally that encloses the return statement.

#### Try-catch 語句Try-catch statements

try try_block
catch(...) catch_block_1
...
catch(...) catch_block_n

• Vtry_block開頭的明確指派狀態，與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of try_block is the same as the definite assignment state of v at the beginning of stmt.
• Vcatch_block_i開頭的明確指派狀態（適用于任何i）與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of catch_block_i (for any i) is the same as the definite assignment state of v at the beginning of stmt.
• 如果在try_block的端點和每個catch_block_i （針對每個i從1到 n）明確指派v ，則會明確指派vstmt結束點的明確指派狀態。 ).The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) v is definitely assigned at the end-point of try_block and every catch_block_i (for every i from 1 to n).

#### Try-finally 語句Try-finally statements

try try_block finally finally_block

• Vtry_block開頭的明確指派狀態，與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of try_block is the same as the definite assignment state of v at the beginning of stmt.
• Vfinally_block開頭的明確指派狀態，與stmt開頭的v明確指派狀態相同。The definite assignment state of v at the beginning of finally_block is the same as the definite assignment state of v at the beginning of stmt.
• 只有在下列其中一項條件成立時，才會明確指派vstmt結束點的明確指派狀態：The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) at least one of the following is true:
• vtry_block的端點明確指派v is definitely assigned at the end-point of try_block
• vfinally_block的端點明確指派v is definitely assigned at the end-point of finally_block

#### Try-catch-finally 語句Try-catch-finally statements

try try_block
catch(...) catch_block_1
...
catch(...) catch_block_n
finally *finally_block*


try {
try try_block
catch(...) catch_block_1
...
catch(...) catch_block_n
}
finally finally_block


class A
{
static void F() {
int i, j;
try {
goto LABEL;
// neither i nor j definitely assigned
i = 1;
// i definitely assigned
}

catch {
// neither i nor j definitely assigned
i = 3;
// i definitely assigned
}

finally {
// neither i nor j definitely assigned
j = 5;
// j definitely assigned
}
// i and j definitely assigned
LABEL:;
// j definitely assigned

}
}


#### Foreach 語句Foreach statements

foreach ( type identifier in expr ) embedded_statement

• Vexpr開頭的明確指派狀態，與stmt開頭的v狀態相同。The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
• V在控制流程傳輸到embedded_statementstmt結束點的明確指派狀態，與expr結尾的v狀態相同。The definite assignment state of v on the control flow transfer to embedded_statement or to the end point of stmt is the same as the state of v at the end of expr.

#### Using 語句Using statements

using ( resource_acquisition ) embedded_statement

• Vresource_acquisition開頭的明確指派狀態，與stmt開頭的v狀態相同。The definite assignment state of v at the beginning of resource_acquisition is the same as the state of v at the beginning of stmt.
• 在控制流程傳輸至embedded_statement上， v的明確指派狀態與resource_acquisition結尾的v狀態相同。The definite assignment state of v on the control flow transfer to embedded_statement is the same as the state of v at the end of resource_acquisition.

#### Lock 語句Lock statements

lock ( expr ) embedded_statement

• Vexpr開頭的明確指派狀態，與stmt開頭的v狀態相同。The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
• 在控制流程傳輸至embedded_statement上， v的明確指派狀態與expr結尾的v狀態相同。The definite assignment state of v on the control flow transfer to embedded_statement is the same as the state of v at the end of expr.

#### Yield 語句Yield statements

yield return expr ;

• Vexpr開頭的明確指派狀態，與stmt開頭的v狀態相同。The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.
• Vstmt結尾的明確指派狀態，與expr結尾的v狀態相同。The definite assignment state of v at the end of stmt is the same as the state of v at the end of expr.
• yield break語句對明確的指派狀態不會有任何影響。A yield break statement has no effect on the definite assignment state.

#### 簡單運算式的一般規則General rules for simple expressions

• 在這類運算式結尾的v明確指派狀態，與運算式開頭的v明確指派狀態相同。The definite assignment state of v at the end of such an expression is the same as the definite assignment state of v at the beginning of the expression.

#### 具有內嵌運算式之運算式的一般規則General rules for expressions with embedded expressions

• e1開頭的v明確指派狀態，與expr開頭的明確指派狀態相同。The definite assignment state of v at the beginning of e1 is the same as the definite assignment state at the beginning of expr.
• Vei開頭的明確指派狀態（i大於1）與前一個子運算式結尾的明確指派狀態相同。The definite assignment state of v at the beginning of ei (i greater than one) is the same as the definite assignment state at the end of the previous sub-expression.
• Vexpr結尾的明確指派狀態，與eN結尾的明確指派狀態相同The definite assignment state of v at the end of expr is the same as the definite assignment state at the end of eN

#### 調用運算式和物件建立運算式Invocation expressions and object creation expressions

primary_expression ( arg1 , arg2 , ... , argN )


new type ( arg1 , arg2 , ... , argN )

• 針對叫用運算式， v之前的primary_expression明確指派狀態與expr**之前的狀態相同。For an invocation expression, the definite assignment state of v before primary_expression is the same as the state of v before expr.
• 針對叫用運算式， v的明確指派狀態在arg1之前，會與primary_expression之後的v狀態相同。For an invocation expression, the definite assignment state of v before arg1 is the same as the state of v after primary_expression.
• 若為物件建立運算式，則在 arg1前面的明確指派狀態會與expr前面的v狀態相同。For an object creation expression, the definite assignment state of v before arg1 is the same as the state of v before expr.
• 針對每個引數argi，在argi之後， v的明確指派狀態是由一般運算式規則所決定refout忽略任何或修飾詞。For each argument argi, the definite assignment state of v after argi is determined by the normal expression rules, ignoring any ref or out modifiers.
• 針對任何i大於1的每個引數argivargi之前的明確指派狀態與上一個arg之後的v狀態相同。For each argument argi for any i greater than one, the definite assignment state of v before argi is the same as the state of v after the previous arg.
• 如果變數v在任何引數中out當做引數傳遞（也就是表單out v的引數），則會明確指派vexpr之後的狀態。If the variable v is passed as an out argument (i.e., an argument of the form out v) in any of the arguments, then the state of v after expr is definitely assigned. 別的v after expr的狀態與 ...argn之後的v狀態相同。Otherwise; the state of v after expr is the same as the state of v after argN.
• 陣列初始化運算式（陣列建立運算式）、物件初始化運算式（物件初始化運算式）、集合初始化運算式（集合初始化運算式）和匿名物件初始化運算式（匿名物件建立）運算式），明確的指派狀態是由根據來定義這些結構的擴充所決定。For array initializers (Array creation expressions), object initializers (Object initializers), collection initializers (Collection initializers) and anonymous object initializers (Anonymous object creation expressions), the definite assignment state is determined by the expansion that these constructs are defined in terms of.

#### 簡單指派運算式Simple assignment expressions

• Vexpr_rhs之前的明確指派狀態，與expr**前面的明確指派狀態相同。The definite assignment state of v before expr_rhs is the same as the definite assignment state of v before expr.
• V after expr之後的明確指派狀態取決於：The definite assignment state of v after expr is determined by:
• 如果wv是相同的變數，則會明確指派v after expr之後的明確指派狀態。If w is the same variable as v, then the definite assignment state of v after expr is definitely assigned.
• 否則，如果指派發生在結構型別的實例函式中，則如果w是屬性存取，在所建立的實例上指定自動實作為屬性P ，而v是的隱藏支援欄位P，則會明確指派v after expr之後的明確指派狀態。Otherwise, if the assignment occurs within the instance constructor of a struct type, if w is a property access designating an automatically implemented property P on the instance being constructed and v is the hidden backing field of P, then the definite assignment state of v after expr is definitely assigned.
• 否則， v after expr之後的明確指派狀態，會與expr_rhs之後之v的明確指派狀態相同。Otherwise, the definite assignment state of v after expr is the same as the definite assignment state of v after expr_rhs.

#### & & （條件式和）運算式&& (conditional AND) expressions

• Vexpr_first之前的明確指派狀態，與expr**前面的明確指派狀態相同。The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
• 如果在expr_first之後的v狀態已明確指派，或「在 true 運算式之後明確指派」，則expr_second 的明確指派狀態會明確指派。The definite assignment state of v before expr_second is definitely assigned if the state of v after expr_first is either definitely assigned or "definitely assigned after true expression". 否則，它不會被明確指派。Otherwise, it is not definitely assigned.
• V after expr之後的明確指派狀態取決於：The definite assignment state of v after expr is determined by:
• 如果expr_first是具有值false的常數運算式，則v after expr的明確指派狀態會與expr_first之後的v明確指派狀態相同。If expr_first is a constant expression with the value false, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr_first.
• 否則，如果已明確指派expr_firstv的狀態，則會將v的狀態明確指派給exprOtherwise, if the state of v after expr_first is definitely assigned, then the state of v after expr is definitely assigned.
• 否則，如果已明確指派expr_secondv的狀態，且expr_first之後的v狀態是「在 false 運算式之後明確指派」，則v的狀態一定會是expr指派.Otherwise, if the state of v after expr_second is definitely assigned, and the state of v after expr_first is "definitely assigned after false expression", then the state of v after expr is definitely assigned.
• 否則，如果expr_second後的v狀態是明確指派的，或「在 true 運算式之後明確指派」，則在expr之後的v狀態會是「在 true 運算式之後明確指派」。Otherwise, if the state of v after expr_second is definitely assigned or "definitely assigned after true expression", then the state of v after expr is "definitely assigned after true expression".
• 否則，如果expr_first之後的v狀態是「在 false 運算式之後明確指派」，而且expr_second之後的v狀態是「在 false 運算式之後明確指派」，則v的狀態會在之後expr是「在 false 運算式之後明確指派」。Otherwise, if the state of v after expr_first is "definitely assigned after false expression", and the state of v after expr_second is "definitely assigned after false expression", then the state of v after expr is "definitely assigned after false expression".
• 否則，在expr之後的v狀態不會明確指派。Otherwise, the state of v after expr is not definitely assigned.

class A
{
static void F(int x, int y) {
int i;
if (x >= 0 && (i = y) >= 0) {
// i definitely assigned
}
else {
// i not definitely assigned
}
// i not definitely assigned
}
}


#### ||（條件式或）運算式|| (conditional OR) expressions

• Vexpr_first之前的明確指派狀態，與expr**前面的明確指派狀態相同。The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
• 如果在expr_first之後的v狀態已明確指派，或「在 false 運算式之後明確指派」，則在expr_second之前的明確指派狀態是明確指派的。The definite assignment state of v before expr_second is definitely assigned if the state of v after expr_first is either definitely assigned or "definitely assigned after false expression". 否則，它不會被明確指派。Otherwise, it is not definitely assigned.
• V after運算式的明確指派語句取決於：The definite assignment statement of v after expr is determined by:
• 如果expr_first是具有值true的常數運算式，則v after expr的明確指派狀態會與expr_first之後的v明確指派狀態相同。If expr_first is a constant expression with the value true, then the definite assignment state of v after expr is the same as the definite assignment state of v after expr_first.
• 否則，如果已明確指派expr_firstv的狀態，則會將v的狀態明確指派給exprOtherwise, if the state of v after expr_first is definitely assigned, then the state of v after expr is definitely assigned.
• 否則，如果已明確指派expr_secondv的狀態，且expr_first之後的v狀態是「在 true 運算式之後明確指派」，則v的狀態一定會是expr指派.Otherwise, if the state of v after expr_second is definitely assigned, and the state of v after expr_first is "definitely assigned after true expression", then the state of v after expr is definitely assigned.
• 否則，如果已明確指派expr_second之後的v狀態，或「在 false 運算式之後明確指派」，則v after expr後面的狀態會是「在 false 運算式之後明確指派」。Otherwise, if the state of v after expr_second is definitely assigned or "definitely assigned after false expression", then the state of v after expr is "definitely assigned after false expression".
• 否則，如果expr_firstv的狀態是「在 true 運算式之後明確指派」，而且expr_second之後的v狀態是「在 true 運算式之後明確指派」，則v的狀態會在expr 之後是「在 true 運算式之後明確指派」。Otherwise, if the state of v after expr_first is "definitely assigned after true expression", and the state of v after expr_second is "definitely assigned after true expression", then the state of v after expr is "definitely assigned after true expression".
• 否則，在expr之後的v狀態不會明確指派。Otherwise, the state of v after expr is not definitely assigned.

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


#### !!（邏輯否定）運算式(logical negation) expressions

• Vexpr_operand之前的明確指派狀態，與expr**前面的明確指派狀態相同。The definite assignment state of v before expr_operand is the same as the definite assignment state of v before expr.
• V after expr之後的明確指派狀態取決於：The definite assignment state of v after expr is determined by:
• 如果 * expr_operand * 之後的v狀態是明確指派的，則會將v的狀態明確指派給expr after。If the state of v after *expr_operand *is definitely assigned, then the state of v after expr is definitely assigned.
• 如果 * expr_operand * 之後的v狀態未明確指派，則不會明確指派v after expr的狀態。If the state of v after *expr_operand *is not definitely assigned, then the state of v after expr is not definitely assigned.
• 如果 * expr_operand * 之後的v狀態是「在 false 運算式之後明確指派」，則v after expr後面的狀態會是「在 true 運算式之後明確指派」。If the state of v after *expr_operand *is "definitely assigned after false expression", then the state of v after expr is "definitely assigned after true expression".
• 如果 * expr_operand * 後面的v狀態是「在 true 運算式之後明確指派」，則v after expr後面的狀態會是「在 false 運算式之後明確指派」。If the state of v after *expr_operand *is "definitely assigned after true expression", then the state of v after expr is "definitely assigned after false expression".

#### ????（null 聯合）運算式(null coalescing) expressions

• Vexpr_first之前的明確指派狀態，與expr**前面的明確指派狀態相同。The definite assignment state of v before expr_first is the same as the definite assignment state of v before expr.
• Vexpr_second之前的明確指派狀態，與expr_first之後的v明確指派狀態相同。The definite assignment state of v before expr_second is the same as the definite assignment state of v after expr_first.
• V after運算式的明確指派語句取決於：The definite assignment statement of v after expr is determined by:
• 如果expr_first是具有值 null 的常數運算式（常數運算式），則v after expr的狀態會與expr_second之後的v狀態相同。If expr_first is a constant expression (Constant expressions) with value null, then the state of v after expr is the same as the state of v after expr_second.
• 否則， v after expr的狀態會與expr_first之後之v的明確指派狀態相同。Otherwise, the state of v after expr is the same as the definite assignment state of v after expr_first.

#### ？：（條件式）運算式?: (conditional) expressions

• Vexpr_cond之前的明確指派狀態，與expr之前的v狀態相同。The definite assignment state of v before expr_cond is the same as the state of v before expr.
• 只有在下列其中一項保留時，才會明確指派expr_true 的明確指派狀態：The definite assignment state of v before expr_true is definitely assigned if and only if one of the following holds:
• expr_cond是具有值的常數運算式falseexpr_cond is a constant expression with the value false
• expr_cond之後的v狀態是明確指派的，或「在 true 運算式之後明確指派」。the state of v after expr_cond is definitely assigned or "definitely assigned after true expression".
• 只有在下列其中一項保留時，才會明確指派expr_false 的明確指派狀態：The definite assignment state of v before expr_false is definitely assigned if and only if one of the following holds:
• expr_cond是具有值的常數運算式trueexpr_cond is a constant expression with the value true
• expr_cond之後的v狀態是明確指派的，或「在 false 運算式之後明確指派」。the state of v after expr_cond is definitely assigned or "definitely assigned after false expression".
• V after expr之後的明確指派狀態取決於：The definite assignment state of v after expr is determined by:
• 如果expr_cond是具有值true的常數運算式（常數運算式），則v after expr的狀態會與expr_true之後的v狀態相同。If expr_cond is a constant expression (Constant expressions) with value true then the state of v after expr is the same as the state of v after expr_true.
• 否則，如果expr_cond是具有值false的常數運算式（常數運算式），則v after expr的狀態會與expr_false之後的v狀態相同。Otherwise, if expr_cond is a constant expression (Constant expressions) with value false then the state of v after expr is the same as the state of v after expr_false.
• 否則，如果已明確指派expr_truev的狀態，且明確指派expr_false 之後的 v 狀態，則會明確指派v after expr的狀態。Otherwise, if the state of v after expr_true is definitely assigned and the state of v after expr_false is definitely assigned, then the state of v after expr is definitely assigned.
• 否則，在expr之後的v狀態不會明確指派。Otherwise, the state of v after expr is not definitely assigned.

#### 匿名函式Anonymous functions

• 外部變數v主體之前的明確指派狀態，與expr之前的v狀態相同。The definite assignment state of an outer variable v before body is the same as the state of v before expr. 也就是說，外部變數的明確指派狀態是繼承自匿名函式的內容。That is, definite assignment state of outer variables is inherited from the context of the anonymous function.
• 外部變數vexpr之後的明確指派狀態，與expr之前的v狀態相同。The definite assignment state of an outer variable v after expr is the same as the state of v before expr.

delegate bool Filter(int i);

void F() {
int max;

// Error, max is not definitely assigned
Filter f = (int n) => n < max;

max = 5;
DoWork(f);
}


delegate void D();

void F() {
int n;
D d = () => { n = 1; };

d();

// Error, n is not definitely assigned
Console.WriteLine(n);
}


## 變數參考Variable references

Variable_reference是分類為變數的運算式A variable_reference is an expression that is classified as a variable. Variable_reference代表可以存取的儲存位置，以提取目前的值並儲存新的值。A variable_reference denotes a storage location that can be accessed both to fetch the current value and to store a new value.

variable_reference
: expression
;