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

I understand passing by reference like this, when use ref keyword, is right? #9111

Closed
CwjXFH opened this issue Nov 19, 2018 · 3 comments
Closed

Comments

@CwjXFH
Copy link

CwjXFH commented Nov 19, 2018

init:

var array = new string[] {"csharp","java","python","go"};
var value = array[0];
ref var refValue = ref array[0];

image

modify value, it will point another object and doesn't effect the original array

value = "dotnet";

image

modify refValue, it point a new object and will change the original array element's value

refValue = "dotnet";

image


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@BillWagner
Copy link
Member

Your understanding is correct @CwjXFH

Closing as the doc is correct.

Copy link

I would like to see an example using a struct as well.

@CwjXFH
Copy link
Author

CwjXFH commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants