Skip to content

Commit e226428

Browse files
authored
Consolidated and revised the ref local reference (#35869)
* Consolidated and revised the ref local reference * Fixed anchors in links * Fix interactive snippet
1 parent d0d8cd8 commit e226428

File tree

14 files changed

+133
-180
lines changed

14 files changed

+133
-180
lines changed

docs/csharp/advanced-topics/performance/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ Using these features to improve performance involves these tasks:
103103
- *Restrict modifications*: When a `struct` type is passed by reference, the called method could modify the state of the struct. You can replace the `ref` modifier with the `in` modifier to indicate that the argument can't be modified. You can also create `readonly struct` types or `struct` types with `readonly` members to provide more control over what members of a `struct` can be modified.
104104
- *Directly manipulate memory*: Some algorithms are most efficient when treating data structures as a block of memory containing a sequence of elements. The `Span` and `Memory` types provide safe access to blocks of memory.
105105

106-
None of these techniques require `unsafe` code. Used wisely, you can get performance characteristics from safe code that was previously only possible by using unsafe techniques. You can try the techniques yourself in the tutorial on [reducing memory allocations](ref-tutorial.md)
106+
None of these techniques require `unsafe` code. Used wisely, you can get performance characteristics from safe code that was previously only possible by using unsafe techniques. You can try the techniques yourself in the tutorial on [reducing memory allocations](ref-tutorial.md).

docs/csharp/language-reference/keywords/ref.md

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ helpviewer_keywords:
88
- "parameters [C#], ref"
99
- "ref keyword [C#]"
1010
---
11-
# ref (C# Reference)
11+
# ref (C# reference)
1212

13-
The `ref` keyword indicates that a variable is a reference, or an alias for another object. It's used in five different contexts:
13+
You use the `ref` keyword in the following contexts:
1414

1515
- In a method signature and in a method call, to pass an argument to a method by reference. For more information, see [Passing an argument by reference](#passing-an-argument-by-reference).
1616
- In a method signature, to return a value to the caller by reference. For more information, see [Reference return values](#reference-return-values).
17-
- In a member body, to indicate that a reference return value is stored locally as a reference that the caller intends to modify. Or to indicate that a local variable accesses another value by reference. For more information, see [Ref locals](#ref-locals).
18-
- In a `struct` declaration, to declare a `ref struct` or a `readonly ref struct`. For more information, see the [`ref struct`](../builtin-types/ref-struct.md) article.
17+
- In a declaration of a local variable, to declare a reference variable. For more information, see the [Reference variables](../statements/declarations.md#reference-variables) section of the [Declaration statements](../statements/declarations.md) article.
18+
- As the part of a [conditional ref expression](../operators/conditional-operator.md#conditional-ref-expression) or a [ref assignment operator](../operators/assignment-operator.md#ref-assignment).
19+
- In a `struct` declaration, to declare a `ref struct`. For more information, see the [`ref struct`](../builtin-types/ref-struct.md) article.
1920
- In a `ref struct` declaration, to declare that a field is a reference. See the [`ref` field](../builtin-types/ref-struct.md) article.
2021

2122
## Passing an argument by reference
@@ -90,45 +91,16 @@ public ref decimal GetCurrentPrice()
9091
return ref DecimalArray[0];
9192
```
9293

93-
In order for the caller to modify the object's state, the reference return value must be stored to a variable that is explicitly defined as a [ref local](#ref-locals).
94+
In order for the caller to modify the object's state, the reference return value must be stored to a variable that is explicitly defined as a [reference variable](../statements/declarations.md#reference-variables).
9495

9596
Here's a more complete ref return example, showing both the method signature and method body.
9697

9798
:::code language="csharp" source="snippets/RefParameterModifier.cs" id="SnippetFindReturningRef":::
9899

99-
The called method may also declare the return value as `ref readonly` to return the value by reference, and enforce that the calling code can't modify the returned value. The calling method can avoid copying the returned value by storing the value in a local [ref readonly](#ref-readonly-locals) variable.
100+
The called method may also declare the return value as `ref readonly` to return the value by reference, and enforce that the calling code can't modify the returned value. The calling method can avoid copying the returned value by storing the value in a local `ref readonly` reference variable.
100101

101102
For an example, see [A ref returns and ref locals example](#a-ref-returns-and-ref-locals-example).
102103

103-
## Ref locals
104-
105-
A ref local variable is used to refer to values returned using `return ref`. A ref local variable can't be initialized to a non-ref return value. In other words, the right-hand side of the initialization must be a reference. Any modifications to the value of the ref local are reflected in the state of the object whose method returned the value by reference.
106-
107-
You define a ref local by using the `ref` keyword in two places:
108-
109-
- Before the variable declaration.
110-
- Immediately before the call to the method that returns the value by reference.
111-
112-
For example, the following statement defines a ref local value that is returned by a method named `GetEstimatedValue`:
113-
114-
```csharp
115-
ref decimal estValue = ref Building.GetEstimatedValue();
116-
```
117-
118-
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 to define a ref local variable that is used to reference a value.
119-
120-
```csharp
121-
ref VeryLargeStruct reflocal = ref veryLargeStruct;
122-
```
123-
124-
In both examples the `ref` keyword must be used in both places, or the compiler generates error CS8172, "Can't initialize a by-reference variable with a value."
125-
126-
The iteration variable of the `foreach` statement can be a ref local or ref readonly local variable. For more information, see the [foreach statement](../statements/iteration-statements.md#the-foreach-statement) article. You can reassign a ref local or ref readonly local variable with the [ref assignment operator](../operators/assignment-operator.md#ref-assignment).
127-
128-
## Ref readonly locals
129-
130-
A ref readonly local is used to refer to values returned by a method or property that has `ref readonly` in its signature and uses `return ref`. A `ref readonly` variable combines the properties of a `ref` local variable with a `readonly` variable: it's an alias to the storage it's assigned to, and it can't be modified.
131-
132104
## A ref returns and ref locals example
133105

134106
The following example defines a `Book` class that has two <xref:System.String> fields, `Title` and `Author`. It also defines a `BookCollection` class that includes a private array of `Book` objects. Individual book objects are returned by reference by calling its `GetBookByTitle` method.
@@ -161,10 +133,6 @@ The `Span<T>` type stores a reference through which it accesses the consecutive
161133

162134
## See also
163135

164-
- [Avoid allocations](../../advanced-topics/performance/index.md)
165-
- [Ref locals](../statements/declarations.md#ref-locals)
166-
- [Conditional ref expression](../operators/conditional-operator.md#conditional-ref-expression)
167-
- [Method Parameters](method-parameters.md)
168-
- [C# Reference](../index.md)
169-
- [C# Programming Guide](../../programming-guide/index.md)
170-
- [C# Keywords](index.md)
136+
- [C# reference](../index.md)
137+
- [C# keywords](index.md)
138+
- [Method parameters](method-parameters.md)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This is called *value assignment*: the value is assigned.
3434

3535
## ref assignment
3636

37-
*Ref assignment* `= ref` makes its left-hand operand an alias to the right-hand operand. The left-hand operand must be a [ref local](../keywords/ref.md#ref-locals), [ref readonly local](../keywords/ref.md#ref-readonly-locals), or a [`ref` field](../builtin-types/ref-struct.md#ref-fields) in a `ref struct`. Both operands must be of the same type.
37+
*Ref assignment* `= ref` makes its left-hand operand an alias to the right-hand operand. The left-hand operand must be a [local reference variable](../statements/declarations.md#reference-variables) or a [`ref` field](../builtin-types/ref-struct.md#ref-fields) in a `ref struct`. Both operands must be of the same type.
3838

3939
The following example demonstrates the usage of the ref assignment operator:
4040

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ a ? b : (c ? d : e)
5353
5454
## Conditional ref expression
5555
56-
A [ref local](../keywords/ref.md#ref-locals) or [ref readonly local](../keywords/ref.md#ref-readonly-locals) variable can be assigned conditionally with a conditional ref expression. You can also use a conditional ref expression as a [reference return value](../keywords/ref.md#reference-return-values) or as a [`ref` method argument](../keywords/ref.md#passing-an-argument-by-reference).
56+
A [reference variable](../statements/declarations.md#reference-variables) can be assigned conditionally with a conditional ref expression. You can also use a conditional ref expression as a [reference return value](../keywords/ref.md#reference-return-values) or as a [`ref` method argument](../keywords/ref.md#passing-an-argument-by-reference).
5757
5858
The syntax for a conditional ref expression is as follows:
5959

0 commit comments

Comments
 (0)