You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/advanced-topics/performance/index.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -103,4 +103,4 @@ Using these features to improve performance involves these tasks:
103
103
-*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.
104
104
-*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.
105
105
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).
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/ref.md
+10-42Lines changed: 10 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,15 @@ helpviewer_keywords:
8
8
- "parameters [C#], ref"
9
9
- "ref keyword [C#]"
10
10
---
11
-
# ref (C# Reference)
11
+
# ref (C# reference)
12
12
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:
14
14
15
15
- 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).
16
16
- 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.
19
20
- In a `ref struct` declaration, to declare that a field is a reference. See the [`ref` field](../builtin-types/ref-struct.md) article.
20
21
21
22
## Passing an argument by reference
@@ -90,45 +91,16 @@ public ref decimal GetCurrentPrice()
90
91
returnrefDecimalArray[0];
91
92
```
92
93
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).
94
95
95
96
Here's a more complete ref return example, showing both the method signature and method body.
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.
100
101
101
102
For an example, see [A ref returns and ref locals example](#a-ref-returns-and-ref-locals-example).
102
103
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`:
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
-
refVeryLargeStructreflocal=refveryLargeStruct;
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
-
132
104
## A ref returns and ref locals example
133
105
134
106
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
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/assignment-operator.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ This is called *value assignment*: the value is assigned.
34
34
35
35
## ref assignment
36
36
37
-
*Refassignment* `=ref` makesitsleft-handoperandanaliastotheright-handoperand. Theleft-handoperandmustbea [reflocal](../keywords/ref.md#ref-locals), [refreadonlylocal](../keywords/ref.md#ref-readonly-locals),ora [`ref` field](../builtin-types/ref-struct.md#ref-fields) ina `ref struct`. Both operands must be of the same type.
37
+
*Refassignment* `=ref` makesitsleft-handoperandanaliastotheright-handoperand. Theleft-handoperandmustbea [localreferencevariable](../statements/declarations.md#reference-variables)ora [`ref` field](../builtin-types/ref-struct.md#ref-fields) ina `refstruct`. Bothoperandsmustbeofthesametype.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/conditional-operator.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ a ? b : (c ? d : e)
53
53
54
54
## Conditional ref expression
55
55
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).
57
57
58
58
The syntax for a conditional ref expression is as follows:
0 commit comments