Skip to content

Commit 512c0ef

Browse files
Fix samples (#42842)
* Fix samples Over time, the samples used in this article (many of which were also used in other articles) drifted from their original purpose. Put those samples in this article only, and make them as small as possible to demonstrate the syntax for disambiguation purposes. Fixes #42499 Fixes #29224 * one more ref... * Update docs/csharp/language-reference/keywords/snippets/refkeyword.cs Co-authored-by: David Pine <[email protected]> --------- Co-authored-by: David Pine <[email protected]>
1 parent d79dc6f commit 512c0ef

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
11
---
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"
44
ms.date: 09/23/2023
55
f1_keywords:
66
- "ref_CSharpKeyword"
77
helpviewer_keywords:
88
- "parameters [C#], ref"
99
- "ref keyword [C#]"
1010
---
11-
# ref (C# reference)
11+
# The `ref` keyword
1212

1313
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](./method-parameters.md#ref-parameter-modifier).
1616

17-
:::code language="csharp" source="./snippets/PassParameters.cs" id="PassByValueOrReference":::
17+
:::code language="csharp" source="./snippets/refKeyword.cs" id="PassByReference":::
1818

1919
- 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).
2020

21-
:::code language="csharp" source="../statements/snippets/jump-statements/ReturnStatement.cs" id="RefReturn":::
21+
:::code language="csharp" source="./snippets/refKeyword.cs" id="ReturnByReference":::
2222

2323
- In a declaration of a local variable, to declare a [reference variable](../statements/declarations.md#reference-variables).
2424

25-
```csharp
26-
ref int aliasOfvariable = ref variable;
27-
```
25+
:::code language="csharp" source="./snippets/refKeyword.cs" id="LocalRef":::
2826

2927
- 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).
3028

31-
:::code language="csharp" source="../operators/snippets/shared/AssignmentOperator.cs" id="SnippetRefAssignment":::
29+
:::code language="csharp" source="./snippets/refKeyword.cs" id="ConditionalRef":::
3230

3331
- In a `struct` declaration, to declare a `ref struct`. For more information, see the [`ref` structure types](../builtin-types/ref-struct.md) article.
3432

35-
:::code language="csharp" source="../builtin-types/snippets/shared/StructType.cs" id="SnippetRefStruct":::
33+
:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefStruct":::
3634

3735
- 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.
3836

39-
:::code language="csharp" source="../builtin-types/snippets/shared/StructType.cs" id="SnippetRefField":::
37+
:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefField":::
4038

4139
- 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.
4240

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":::
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class RefKeywordExamples
2+
{
3+
// <PassByReference>
4+
public void M(ref int refParameter)
5+
{
6+
refParameter += 42;
7+
}
8+
// </PassByReference>
9+
10+
// <ReturnByReference>
11+
public ref int RefMax(ref int left, ref int right)
12+
{
13+
if (left > right)
14+
{
15+
return ref left;
16+
}
17+
else
18+
{
19+
return ref right;
20+
}
21+
}
22+
// </ReturnByReference>
23+
24+
// <LocalRef>
25+
public void M2(int variable)
26+
{
27+
ref int aliasOfvariable = ref variable;
28+
}
29+
// </LocalRef>
30+
31+
// <ConditionalRef>
32+
public ref int RefMaxConditions(ref int left, ref int right)
33+
{
34+
ref int returnValue = ref left > right ? ref left : ref right;
35+
return ref returnValue;
36+
}
37+
// </ConditionalRef>
38+
}
39+
40+
// <SnippetRefStruct>
41+
public ref struct CustomRef
42+
{
43+
public ReadOnlySpan<int> Inputs;
44+
public ReadOnlySpan<int> Outputs;
45+
}
46+
// </SnippetRefStruct>
47+
48+
// <SnippetRefField>
49+
public ref struct RefFieldExample
50+
{
51+
private ref int number;
52+
}
53+
// </SnippetRefField>
54+
55+
// <SnippetRefGeneric>
56+
class RefStructGeneric<T, S>
57+
where T : allows ref struct
58+
where S : T
59+
{
60+
// etc
61+
}
62+
// </SnippetRefGeneric>

0 commit comments

Comments
 (0)