Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix samples #42842

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions docs/csharp/language-reference/keywords/ref.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
---
description: "ref keyword - C# Reference"
title: "ref keyword"
description: "Understand the different uses for the `ref` keyword and get more information on those uses"
title: "The multiple uses of the `ref` keyword"
ms.date: 09/23/2023
f1_keywords:
- "ref_CSharpKeyword"
helpviewer_keywords:
- "parameters [C#], ref"
- "ref keyword [C#]"
---
# ref (C# reference)
# The `ref` keyword

You use the `ref` keyword in the following contexts:

- In a method signature and in a method call, to pass an argument to a method [by reference](./method-parameters.md#ref-parameter-modifier).

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

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

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

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

```csharp
ref int aliasOfvariable = ref variable;
```
:::code language="csharp" source="./snippets/refKeyword.cs" id="LocalRef":::

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

:::code language="csharp" source="../operators/snippets/shared/AssignmentOperator.cs" id="SnippetRefAssignment":::
:::code language="csharp" source="./snippets/refKeyword.cs" id="ConditionalRef":::

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

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

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

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

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

```csharp
class SomeClass<T, S>
where T : allows ref struct
where S : T
{
// etc
}
```
:::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefGeneric":::
62 changes: 62 additions & 0 deletions docs/csharp/language-reference/keywords/snippets/refkeyword.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
public class RefKeywordExamples
{
// <PassByReference>
public void M(ref int refParameter)
{
refParameter += 42;
}
// </PassByReference>

// <ReturnByReference>
public ref int RefMax(ref int left, ref int right)
{
if (left > right)
{
return ref left;
}
else
{
return ref right;
}
}
// </ReturnByReference>

// <LocalRef>
public void M2(int variable)
{
ref int aliasOfvariable = ref variable;
}
// </LocalRef>

// <ConditionalRef>
public ref int RefMaxConditions(ref int left, ref int right)
{
ref int returnValue = ref left > right ? ref left : ref right;
return ref returnValue;
}
// </ConditionalRef>
}

// <SnippetRefStruct>
public ref struct CustomRef
{
public ReadOnlySpan<int> Inputs;
public ReadOnlySpan<int> Outputs;
}
// </SnippetRefStruct>

// <SnippetRefField>
public ref struct RefFieldExample
{
private ref int number;
}
// </SnippetRefField>

// <SnippetRefGeneric>
class RefStructGeneric<T, S>
where T : allows ref struct
where S : T
{
// etc
}
// </SnippetRefGeneric>
Loading