|
1 | 1 | --- |
2 | | -description: "ref keyword - C# Reference" |
3 | | -title: "ref keyword" |
| 2 | +description: "Understand the different uses for the `ref` keyword and get more information on those uses" |
| 3 | +title: "The multiple uses of the `ref` keyword" |
4 | 4 | ms.date: 09/23/2023 |
5 | 5 | f1_keywords: |
6 | 6 | - "ref_CSharpKeyword" |
7 | 7 | helpviewer_keywords: |
8 | 8 | - "parameters [C#], ref" |
9 | 9 | - "ref keyword [C#]" |
10 | 10 | --- |
11 | | -# ref (C# reference) |
| 11 | +# The `ref` keyword |
12 | 12 |
|
13 | 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](./method-parameters.md#ref-parameter-modifier). |
16 | 16 |
|
17 | | -:::code language="csharp" source="./snippets/PassParameters.cs" id="PassByValueOrReference"::: |
| 17 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="PassByReference"::: |
18 | 18 |
|
19 | 19 | - In a method signature, to return a value to the caller by reference. For more information, see [`ref return`](../statements/jump-statements.md#ref-returns). |
20 | 20 |
|
21 | | -:::code language="csharp" source="../statements/snippets/jump-statements/ReturnStatement.cs" id="RefReturn"::: |
| 21 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="ReturnByReference"::: |
22 | 22 |
|
23 | 23 | - In a declaration of a local variable, to declare a [reference variable](../statements/declarations.md#reference-variables). |
24 | 24 |
|
25 | | -```csharp |
26 | | -ref int aliasOfvariable = ref variable; |
27 | | -``` |
| 25 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="LocalRef"::: |
28 | 26 |
|
29 | 27 | - 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). |
30 | 28 |
|
31 | | -:::code language="csharp" source="../operators/snippets/shared/AssignmentOperator.cs" id="SnippetRefAssignment"::: |
| 29 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="ConditionalRef"::: |
32 | 30 |
|
33 | 31 | - In a `struct` declaration, to declare a `ref struct`. For more information, see the [`ref` structure types](../builtin-types/ref-struct.md) article. |
34 | 32 |
|
35 | | -:::code language="csharp" source="../builtin-types/snippets/shared/StructType.cs" id="SnippetRefStruct"::: |
| 33 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefStruct"::: |
36 | 34 |
|
37 | 35 | - In a `ref struct` definition, to declare a `ref` field. For more information, see the [`ref` fields](../builtin-types/ref-struct.md#ref-fields) section of the [`ref` structure types](../builtin-types/ref-struct.md) article. |
38 | 36 |
|
39 | | -:::code language="csharp" source="../builtin-types/snippets/shared/StructType.cs" id="SnippetRefField"::: |
| 37 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefField"::: |
40 | 38 |
|
41 | 39 | - In a generic type declaration to specify that a type parameter [`allows ref struct`](../../programming-guide/generics/constraints-on-type-parameters.md#allows-ref-struct) types. |
42 | 40 |
|
43 | | -```csharp |
44 | | -class SomeClass<T, S> |
45 | | - where T : allows ref struct |
46 | | - where S : T |
47 | | -{ |
48 | | - // etc |
49 | | -} |
50 | | -``` |
| 41 | +:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefGeneric"::: |
0 commit comments