Beginner question regarding reference.

So lets say I have a method called Plus that takes an int i and then does i++;

For an example, I’m going to pass through a already made int called a that = 5 as an argument.

The int i is only increased by one within the scope of the method, so if I was to Console.WriteLine(i) within the method it would return 6 whereas if I was to Plus(a) and then Console.WriteLine(a) it would return the original value 5 as the method affected the copy of a it made within the scope(am I right in thinking this?).

Now using the ref keyword on both the method and the calling of the method Plus(ref a), makes it so that the method now affects values outside the scope. The way I’ve read this works it that instead of creating a copy of the value passed into the method it just refers (hence ref) to original version/location of whats passed into it.

How exactly does this work or is it really that simple?

submitted by /u/TechieToch
[link] [comments]

Leave a Reply