赋值运算符(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 localref readonly local 变量。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 时,才能使用 null 合并赋值运算符 ??= 将其右操作数的值分配给左操作数。Beginning 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. 但是,如果用户定义类型重载了二元运算符 op,则 op= 运算符(如果存在)也将被隐式重载。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