This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
The assignment operator=
assigns thevalue of its right-hand operand to a variable, aproperty, or anindexer 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:
List<double> numbers = [1.0, 2.0, 3.0];Console.WriteLine(numbers.Capacity);numbers.Capacity = 100;Console.WriteLine(numbers.Capacity);// Output:// 4// 100int newFirstElement;double originalFirstElement = numbers[0];newFirstElement = 5;numbers[0] = newFirstElement;Console.WriteLine(originalFirstElement);Console.WriteLine(numbers[0]);// Output:// 1// 5
The left-hand operand of an assignment receives thevalue of the right-hand operand. When the operands are ofvalue types, assignment copies the contents of the right-hand operand. When the operands are ofreference types, assignment copies the reference to the object.
This operation is calledvalue assignment: the value is assigned.
Beginning with C# 14, the left hand side of a value assignment can include anull conditional member expression, such as?.
or?[]
. If the left hand side is null, the right hand side expression isn't evaluated.
Ref assignment= ref
makes its left-hand operand an alias to the right-hand operand, as the following example demonstrates:
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
In the preceding example, thelocal reference variablearrayElement
is initialized as an alias to the first array element. Then, it'sref
reassigned to refer to the last array element. As it's an alias, when you update its value with an ordinary assignment operator=
, the corresponding array element is also updated.
The left-hand operand ofref
assignment can be alocal reference variable, aref
field, and aref
,out
, orin
method parameter. Both operands must be of the same type.
Aref
assignment means that a reference variable has a different referrent. It's no longer referring to its previous referrent. Usingref =
on aref
parameter means the parameter no longer refers to its argument. Any actions that modify the state of the object after ref reassigning it make those modifications to the new item. For example, consider the following method:
private static void RefReassignAndModify(scoped ref string s){ string sLocal = "Hello"; Console.WriteLine(sLocal); // Output: Hello s = ref sLocal; s = "World"; Console.WriteLine(s); // Output: World
The following usage shows that the assignment to the parameters
isn't visible after the method call becauses
wasref
reassigned to refer tosLocal
before the string was modified:
string msg = "Hi";RefReassignAndModify(ref msg);Console.WriteLine(msg); // Output: Hi!
For a binary operatorop
, a compound assignment expression of the form
x op= y
Is equivalent to
x = x op y
Except thatx
is only evaluated once.
Thearithmetic,Boolean logical, andbitwise logical and shift operators all support compound assignment.
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 tonull
. For more information, see the??
and??=
operators article.
A user-defined type can'toverload 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, seeUser-defined conversion operators.
If a user-defined type overloads a binary operatorop
, theop=
operator, if it exists, is also implicitly overloaded. Beginning with C# 14, a user-defined type can explicitly overload the compound assignment operators (op=
) to provide a more efficient implementation. Typically, a type overloads these operators because the value can be updated in place, rather than allocating a new instance to hold the result of the binary operation. If a type doesn't provide an explicit overload, the compiler generates the implicit overload.
For more information, see theAssignment operators section of theC# language specification and theUser defined compound assignment feature specification.
Was this page helpful?
Was this page helpful?