Suppose a method is changing the value of an argument passed by reference. Is the effect of such operation immediately visible across the application or only after the method returns?
Below an example when this is significant:
int x = 0;
void Foo(ref int y)
{
++y;
Console.WriteLine(x);
}
Foo(ref x);
It can be run in C# Pad under http://csharppad.com/gist/915318e2cc0da2c2533dfa7983119869
The function Foo
has access to the variable x
because it is in the same scope and also it just so happens to receive a reference to it at call site. If the effect of ++y
is immediate, the output should be 1
, but I can imagine a compiler to generate code that, for example, stores the local value in a register and dumps to memory at some later time before return. Does the language specification ensure the output to be 1
or does it allow the jitter to optimize, making the output implementation dependent?