Skip to content

Latest commit

 

History

History
28 lines (26 loc) · 2.1 KB

File metadata and controls

28 lines (26 loc) · 2.1 KB
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 01/22/2026
f1_keywords
ref_CSharpKeyword
helpviewer_keywords
parameters [C#], ref
ref keyword [C#]

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. :::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. :::code language="csharp" source="./snippets/refKeyword.cs" id="ReturnByReference":::
  • In a declaration of a local variable, to declare a reference variable. :::code language="csharp" source="./snippets/refKeyword.cs" id="LocalRef":::
  • As the part of a conditional ref expression or a ref assignment operator. :::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 article. :::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 section of the ref structure types article. :::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefField":::
  • In a generic type declaration to specify that a type parameter allows ref struct types. :::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefGeneric":::