指派運算子 (c # 參考) Assignment operators (C# reference)

指派運算子 = 會將其右方運算元的值指派給其左方運算元所指定的變數、屬性索引子元素。The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. 指派運算式的結果是指派給左方運算元的值。The result of an assignment expression is the value assigned to the left-hand operand. 右方運算元的型別必須與左方運算元的型別相同,或是會隱含地轉換成該型別。The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.

指派運算子 = 是右向關聯,也就是表單的運算式The assignment operator = is right-associative, that is, an expression of the form

a = b = c

會評估為is evaluated as

a = (b = c)

下列範例示範將區域變數、屬性及索引子元素作為指派運算子左運算元的用法:The following example demonstrates the usage of the assignment operator with a local variable, a property, and an indexer element as its left-hand operand:

var numbers = new List<double>() { 1.0, 2.0, 3.0 };

Console.WriteLine(numbers.Capacity);
numbers.Capacity = 100;
Console.WriteLine(numbers.Capacity);
// Output:
// 4
// 100

int newFirstElement;
double originalFirstElement = numbers[0];
newFirstElement = 5;
numbers[0] = newFirstElement;
Console.WriteLine(originalFirstElement);
Console.WriteLine(numbers[0]);
// Output:
// 1
// 5

ref 指派運算子ref assignment operator

從 C# 7.3 開始,您可以使用 ref 指派運算子 = ref 來重新指派 ref 區域變數ref readonly 區域變數Beginning with C# 7.3, you can use the ref assignment operator = ref to reassign a ref local or ref readonly local variable. 下列範例示範 ref 指派運算子的用法:The following example demonstrates the usage of the ref assignment operator:

void Display(double[] s) => Console.WriteLine(string.Join(" ", s));

double[] arr = { 0.0, 0.0, 0.0 };
Display(arr);

ref double arrayElement = ref arr[0];
arrayElement = 3.0;
Display(arr);

arrayElement = ref arr[arr.Length - 1];
arrayElement = 5.0;
Display(arr);
// Output:
// 0 0 0
// 3 0 0
// 3 0 5

在 ref 指派運算子的案例中,它的兩個運算元都必須是相同的類型。In the case of the ref assignment operator, the both of its operands must be of the same type.

複合指派Compound assignment

若是二元運算子 op,表單的複合指派運算式For a binary operator op, a compound assignment expression of the form

x op= y

相當於is equivalent to

x = x op y

但只會評估 x 一次。except that x is only evaluated once.

算數布林邏輯位元邏輯與位移運算子支援複合指派。Compound assignment is supported by arithmetic, Boolean logical, and bitwise logical and shift operators.

Null 聯合指派Null-coalescing assignment

從 c # 8.0 開始,您可以使用 null 聯合指派運算子,將右邊運算元的值指派給左邊的運算元( ??= 如果左邊運算元評估為) nullBeginning with C# 8.0, you can use the null-coalescing assignment operator ??= to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. 如需詳細資訊,請參閱 ??和?= 運算子 文章。For more information, see the ?? and ??= operators article.

運算子是否可多載Operator overloadability

使用者定義型 別無法多 載指派運算子。A user-defined type cannot overload the assignment operator. 不過,使用者定義型別可以定義會轉換成另一型別的隱含轉換。However, a user-defined type can define an implicit conversion to another type. 如此一來,便可將使用者定義型別的值指派給另一型別的變數、屬性或索引子元素。That way, the value of a user-defined type can be assigned to a variable, a property, or an indexer element of another type. 如需詳細資訊,請參閱使用者定義轉換運算子For more information, see User-defined conversion operators.

使用者定義型別無法明確地多載複合指派運算子。A user-defined type cannot explicitly overload a compound assignment operator. 不過,如果使用者定義型別多載二元運算子 opop= 運算子(如果存在的話)也會隱含地多載。However, if a user-defined type overloads a binary operator op, the op= operator, if it exists, is also implicitly overloaded.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格指派運算子一節。For more information, see the Assignment operators section of the C# language specification.

如需 ref 指派運算子的詳細資訊 = ref ,請參閱 功能提案注意事項For more information about the ref assignment operator = ref, see the feature proposal note.

另請參閱See also