| 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 |
|
||
| helpviewer_keywords |
|
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
structdeclaration, to declare aref struct. For more information, see therefstructure types article. :::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefStruct"::: - In a
ref structdefinition, to declare areffield. For more information, see thereffields section of therefstructure types article. :::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefField"::: - In a generic type declaration to specify that a type parameter
allows ref structtypes. :::code language="csharp" source="./snippets/refKeyword.cs" id="SnippetRefGeneric":::