C# Reference ยท Operators and Expressions
C# assignment operators
Reference for C# assignment operators: simple assignment, compound forms like += and <<=, ref assignment, chained assignment and implicit conversions.
The assignment operator = stores the value of its right operand in the variable, property, indexer or element on its left. An assignment is an expression: it produces the assigned value, which is why a = b = 0 works. It is right-associative, so it reads as a = (b = 0).
Compound assignment combines an operator with assignment: x += 3 means x = x + 3, except that x is evaluated only once. Every binary arithmetic, bitwise and shift operator has a compound form, and ??= assigns only when the left side is null.
int total = 10; total += 5; // 15 total *= 2; // 30 total %= 7; // 2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
target = value; target op= value; // op is + - * / % & | ^ << >> >>> ?? ref target = ref variable; // ref assignment (a, b) = (b, a); // deconstructing assignment
| Operator | Meaning |
|---|---|
= |
Simple assignment. |
+=, -=, *=, /=, %= |
Arithmetic compound assignment. += and -= also subscribe and unsubscribe events. |
&=, |=, ^= |
Bitwise or logical compound assignment. |
<<=, >>=, >>>= |
Shift compound assignment. |
??= |
Assign only if the left side is null (the right side is not evaluated otherwise). |
= ref |
Make a ref local or ref field refer to another variable. |
Returns: the value that was assigned, with the type of the left operand.
Errors: assigning a wider type without a cast is a compile error (int to short, double to int). Compound assignment is the exception: small += 100 includes an implicit cast back to the type of the left side when the operator's result type is convertible with an explicit conversion and the right side is implicitly convertible.
Usage notes
- Single evaluation. In
values[Next()] += 1,Next()runs once. Writingvalues[Next()] = values[Next()] + 1would call it twice. - Assignment copies a value or a reference. For structs,
=copies the whole value. For classes,=copies the reference so both variables see the same object. Strings are references but immutable, sos += "x"creates a new string each time. - Do not use
=where==is meant.if (a = b)only compiles whenais abool; use==for comparison. - Deconstruction assigns several targets at once:
(x, y) = (y, x)swaps without a temporary, andvar (id, name) = person;declares new variables. - Discards.
_ = Compute();explicitly ignores a result;(_, var count) = Pair();ignores part of a tuple. - Compound operators on events (
handler += OnClick) are not arithmetic; they call the event's add and remove accessors. - Readonly targets. You cannot assign to a
readonlyfield outside a constructor, aconst, aninit-only property after initialization, or aforeachiteration variable. - Custom types can declare
+=-style operators directly since C# 14 (in-place compound assignment). Without such a declaration,x += ycalls the ordinary+operator and assigns the result. - Performance: repeated
s += ...on strings in a loop is quadratic. UseStringBuilderorstring.Join.