Closed
Description
init:
var array = new string[] {"csharp","java","python","go"};
var value = array[0];
ref var refValue = ref array[0];
modify value, it will point another object and doesn't effect the original array
value = "dotnet";
modify refValue, it point a new object and will change the original array element's value
refValue = "dotnet";
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 3d67349b-72bc-b7cf-f754-fa62d1fb592d
- Version Independent ID: 4f413bcc-90e7-d971-fe75-b4876124e75d
- Content: ref keyword (C# Reference)
- Content Source: docs/csharp/language-reference/keywords/ref.md
- Product: dotnet-csharp
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn
Activity
BillWagner commentedon Nov 30, 2018
Your understanding is correct @CwjXFH
Closing as the doc is correct.
FrankHileman commentedon Apr 21, 2020
I would like to see an example using a struct as well.
CwjXFH commentedon Apr 22, 2020
@FrankHileman
Maybe this example will help you, in the picture below, refValue passed by ref, has the same address and value with num; argument value passed by value, it just has the same value with num, the address is different.
And recommended that you read the following document:
Passing Parameters (C# Programming Guide)
Method Parameters (C# Reference)
The address-of operator