From f8fcc77519f20817c73060b6a0749eb3a3b0e166 Mon Sep 17 00:00:00 2001 From: David Pine Date: Wed, 22 Apr 2020 09:58:18 -0500 Subject: [PATCH] Added a more complete ref return example, using an int[,]. --- docs/csharp/language-reference/keywords/ref.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/keywords/ref.md b/docs/csharp/language-reference/keywords/ref.md index ba452635ba5d8..aa89125def582 100644 --- a/docs/csharp/language-reference/keywords/ref.md +++ b/docs/csharp/language-reference/keywords/ref.md @@ -71,7 +71,7 @@ For more information about how to pass reference types by value and by reference ## Reference return values -Reference return values (or ref returns) are values that a method returns by reference to the caller. That is, the caller can modify the value returned by a method, and that change is reflected in the state of the object that contains the method. +Reference return values (or ref returns) are values that a method returns by reference to the caller. That is, the caller can modify the value returned by a method, and that change is reflected in the state of the object in the calling method. A reference return value is defined by using the `ref` keyword: @@ -89,6 +89,10 @@ return ref DecimalArray[0]; In order for the caller to modify the object's state, the reference return value must be stored to a variable that is explicitly defined as a [ref local](#ref-locals). +Here is a more complete ref return example, showing both the method signature and method body. + +[!code-csharp[FindReturningRef](~/samples/snippets/csharp/new-in-7/MatrixSearch.cs#FindReturningRef "Find returning by reference")] + The called method may also declare the return value as `ref readonly` to return the value by reference, and enforce that the calling code cannot modify the returned value. The calling method can avoid copying the returned valued by storing the value in a local [ref readonly](#ref-readonly-locals) variable. For an example, see [A ref returns and ref locals example](#a-ref-returns-and-ref-locals-example).