Skip to content

Commit c8293d4

Browse files
committed
add ref reassignment
Point out that if you `ref =` assign a parameter to new storage, any subsequent value changes aren't visible outside the method.
1 parent 283d94e commit c8293d4

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

docs/csharp/language-reference/operators/assignment-operator.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Assignment operators - assign an expression to a variable"
3-
description: "C# assignment operators assign an expression to a variable. Assignment sets the value of the expression. `ref` assignment sets the reference of a `ref` variable."
4-
ms.date: 11/29/2022
3+
description: "C# Assignment sets the value of the expression. Alternatively, `ref` assignment sets the reference of a reference variable."
4+
ms.date: 11/19/2024
55
f1_keywords:
66
- "=_CSharpKeyword"
77
helpviewer_keywords:
@@ -18,7 +18,7 @@ The assignment operator `=` is right-associative, that is, an expression of the
1818
a = b = c
1919
```
2020

21-
is evaluated as
21+
Is evaluated as
2222

2323
```csharp
2424
a = (b = c)
@@ -30,7 +30,7 @@ The following example demonstrates the usage of the assignment operator with a l
3030

3131
The left-hand operand of an assignment receives the *value* of the right-hand operand. When the operands are of [value types](../builtin-types/value-types.md), assignment copies the contents of the right-hand operand. When the operands are of [reference types](../builtin-types/reference-types.md), assignment copies the reference to the object.
3232

33-
This is called *value assignment*: the value is assigned.
33+
This operation is called *value assignment*: the value is assigned.
3434

3535
## ref assignment
3636

@@ -42,6 +42,14 @@ In the preceding example, the [local reference variable](../statements/declarati
4242

4343
The left-hand operand of `ref` assignment can be a [local reference variable](../statements/declarations.md#reference-variables), a [`ref` field](../builtin-types/ref-struct.md#ref-fields), and a [`ref`](../keywords/ref.md), [`out`](../keywords/method-parameters.md#out-parameter-modifier), or [`in`](../keywords/method-parameters.md#in-parameter-modifier) method parameter. Both operands must be of the same type.
4444

45+
A `ref` assignment means that a reference variable has a different referrent. It's no longer referring to its previous referrent. Using `ref =` on a `ref` 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:
46+
47+
:::code language="csharp" source="snippets/shared/AssignmentOperator.cs" id="SnippetRefReassignAndModify":::
48+
49+
The following usage shows that the assignment to the parameter `s` isn't visible after the method call because `s` was `ref` reassigned to refer to `sLocal` before the string was modified:
50+
51+
:::code language="csharp" source="snippets/shared/AssignmentOperator.cs" id="Usage":::
52+
4553
## Compound assignment
4654

4755
For a binary operator `op`, a compound assignment expression of the form
@@ -50,15 +58,15 @@ For a binary operator `op`, a compound assignment expression of the form
5058
x op= y
5159
```
5260

53-
is equivalent to
61+
Is equivalent to
5462

5563
```csharp
5664
x = x op y
5765
```
5866

59-
except that `x` is only evaluated once.
67+
Except that `x` is only evaluated once.
6068

61-
Compound assignment is supported by [arithmetic](arithmetic-operators.md#compound-assignment), [Boolean logical](boolean-logical-operators.md#compound-assignment), and [bitwise logical and shift](bitwise-and-shift-operators.md#compound-assignment) operators.
69+
The [arithmetic](arithmetic-operators.md#compound-assignment), [Boolean logical](boolean-logical-operators.md#compound-assignment), and [bitwise logical and shift](bitwise-and-shift-operators.md#compound-assignment) operators all support compount assignment.
6270

6371
## Null-coalescing assignment
6472

docs/csharp/language-reference/operators/snippets/shared/AssignmentOperator.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public static void Examples()
66
{
77
Simple();
88
RefAssignment();
9+
// <Usage>
10+
string msg = "Hi";
11+
RefReassignAndModify(ref msg);
12+
Console.WriteLine(msg); // Output: Hi!
13+
// </Usage>
914
}
1015

1116
private static void Simple()
@@ -53,4 +58,16 @@ private static void RefAssignment()
5358
// 3 0 5
5459
// </SnippetRefAssignment>
5560
}
61+
62+
// <SnippetRefReassignAndModify>
63+
private static void RefReassignAndModify(scoped ref string s)
64+
{
65+
string sLocal = "Hello";
66+
Console.WriteLine(sLocal); // Output: Hello
67+
68+
s = ref sLocal;
69+
s = "World";
70+
Console.WriteLine(s); // Output: World
71+
// </SnippetRefReassignAndModify>
72+
}
5673
}

0 commit comments

Comments
 (0)