1

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?

1
  • 1
    This give the optimizer built into the just-in-time compiler a migraine. But no surprises in any of the current versions of the jitter, they are well aware that the argument might be an alias and are pessimistic about it. One reason that Fortran is still around, such aliasing is explicit. The strict aliasing rule change in GCC caused a lot of havoc. Commented Jan 11, 2017 at 16:49

3 Answers 3

7

Is the effect of such operation immediately visible across the application or only after the method returns?

It's immediately visible - because basically, what you end up passing is the variable itself, not the variable's value. You're modifying the exact same storage location.

Indeed, you can see this within the same same method:

using System;

class Test
{
    static void Main(string[] args)
    {
        int a = 10;
        Foo(ref a, ref a);
    }

    static void Foo(ref int x, ref int y)
    {
        x = 2;
        Console.WriteLine(y); // Prints 2, because x and y share a storage location
    }
}

This is in the C# 5 spec in section 5.1.5:

A reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the function member or anonymous function invocation. Thus, the value of a reference parameter is always the same as the underlying variable.

The same is true in reverse, by the way - if the value of the underlying variable is changed in some other way, that change will be visible in the method. Example using a delegate to change the value:

using System;

class Test
{
    static void Main(string[] args)
    {
        int a = 10;
        Foo(ref a, () => a++);
    }

    static void Foo(ref int x, Action action)
    {
        Console.WriteLine(x); // 10
        action();             // Changes the value of a
        Console.WriteLine(x); // 11
        x = 5;
        action();
        Console.WriteLine(x); // 6
    }
}
2
  • A clarification of what the storage location means would be appreciated. I understand that it is a conceptual location in the abstract machine, and not the physical location storing the value of the variable, which almost certainly is spanned across registers, caches and operating memory. Commented Jan 11, 2017 at 17:03
  • 1
    @MarcinKaczmarek: Yes, that's about right - it's the notional place that the data is stored. I wouldn't like to specify it much more clearly than that :)
    – Jon Skeet
    Commented Jan 11, 2017 at 17:36
2

ref is an alias for a storage location. The ref argument points to the exact same variable you passed in, so yes, assignments are visible immediately.

1

The C# spec guarantees that all operations must be observed to have happened in order when running in a single threaded context. So it would be an invalid optimization for your provided program to output 0 as that would result in a reordering observed from a single thread.

When you provide a ref parameter the whole point is that the parameter is an alias for the referenced variable. It is not a copy; the changes are not to be observed only after the method completes. Rather, any usage of y in your program is semantically identical to using x because both identifiers reference the same storage location.

I'll note that your program only ever accesses the variable after the method using the ref parameter returns, so it doesn't actually answer your question. A program snippet that actually changes based on whether the ref parameter is actually a reference to the same variable, or if it simply copies the value back at the end of the method, would look something like this:

public static void Foo(ref int y, Func<int> function)
{
    y = 42;
    Console.WriteLine(function());
}

int x = 7;
Foo(ref x, () => x);
1
  • I admit that the time of accessing the variable was arguable, so I updated the snippet. Commented Jan 11, 2017 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.