|
1 | 1 | ---
|
2 | 2 | title: "Declaration statements - var, ref local variables, and ref fields"
|
3 | 3 | description: "Declarations introduce a new variable. These statements include `var`, `ref` locals, and `ref` fields. In addition to declaring a new variable, these statements can initialize that variable's value."
|
4 |
| -ms.date: 11/22/2022 |
| 4 | +ms.date: 01/30/2023 |
5 | 5 | f1_keywords:
|
6 | 6 | - "var"
|
7 | 7 | - "var_CSharpKeyword"
|
@@ -45,27 +45,22 @@ The following example shows two query expressions. In the first expression, the
|
45 | 45 |
|
46 | 46 | ## Ref locals
|
47 | 47 |
|
48 |
| -You add the `ref` keyword before the type of a variable to declare a `ref` local. Assume the `GetContactInformation` method is declared as a [ref return](jump-statements.md#ref-returns): |
| 48 | +You add the `ref` keyword before the type of a variable to declare a `ref` local. A `ref` local is a variable that *refers to* other storage. Assume the `GetContactInformation` method is declared as a [ref return](jump-statements.md#ref-returns): |
49 | 49 |
|
50 | 50 | ```csharp
|
51 | 51 | public ref Person GetContactInformation(string fname, string lname)
|
52 | 52 | ```
|
53 | 53 |
|
54 |
| -A by-value assignment reads the value of a variable and assigns it to a new variable: |
| 54 | +Let's contrast these two assignments: |
55 | 55 |
|
56 | 56 | ```csharp
|
57 | 57 | Person p = contacts.GetContactInformation("Brandie", "Best");
|
| 58 | +ref Person p2 = ref contacts.GetContactInformation("Brandie", "Best"); |
58 | 59 | ```
|
59 | 60 |
|
60 |
| -The preceding assignment declares `p` as a local variable. Its initial value is copied from reading the value returned by `GetContactInformation`. Any future assignments to `p` won't change the value of the variable returned by `GetContactInformation`. The variable `p` is no longer an alias to the variable returned. |
| 61 | +The variable `p` holds a *copy* of the return value from `GetContactInformation`. It's a separate storage location from the `ref` return from `GetContactInformation`. If you change any property of `p`, you are changing a copy of the `Person`. |
61 | 62 |
|
62 |
| -You declare a *ref* variable to copy the alias to the original value. In the following assignment, `p` is an alias to the variable returned from `GetContactInformation`. |
63 |
| - |
64 |
| -```csharp |
65 |
| -ref Person p = ref contacts.GetContactInformation("Brandie", "Best"); |
66 |
| -``` |
67 |
| - |
68 |
| -Subsequent usage of `p` is the same as using the variable returned by `GetContactInformation` because `p` is an alias for that variable. Changes to `p` also change the variable returned from `GetContactInformation`. |
| 63 | +The variable `p2` *refers to* the storage location for the `ref` return from `GetContactInformation`. It's the same storage as the `ref` return from `GetContactInformation`. If you change any property of `p2`, you are changing that single instance of a `Person`. |
69 | 64 |
|
70 | 65 | You can access a value by reference in the same way. In some cases, accessing a value by reference increases performance by avoiding a potentially expensive copy operation. For example, the following statement shows how one can define a ref local value that is used to reference a value.
|
71 | 66 |
|
|
0 commit comments