tags:

views:

201

answers:

2

When I started using C# I was unsure of how references were treated exactly (whether they were being passed by value etc.). I wrongly thought the 'ref' keyword was needed when passing objects that would be modified by the called method.

Then after reading threads like this, I realized 'ref' was only needed when you need to change the actual reference / pointer itself.

But today I have come across an issue when passing a parameter via a remoting call, where ref was actually needed to modify the content of the object. When passed without ref, the object came back unchanged. I was told to add the ref keyword but I argued for a while that it was only necessary when you change the pointer itself, not the content that is being pointed to.

I have searched the net and could only find a single page that discusses it briefly. Is this a known issue and is anyone able to point to some documentation about it? It seems to me that I will have to use ref now for any parameter that is being modified via a remoting call.

+1  A: 

I wonder why did you say "reference/pointer"?. There's a big difference between those terms. See, a pointer is just an address, say an int.

On the other hand, a reference is nothing but an alias to something. In terms of C++:

int x;
int& y = x;

From here on, whatever happens to x, happens to y and viceversa, they are bound "forever".

Again, in C++, a pass-by-ref:

void foo(int& y);
int main(){
    int x = 0;
    foo(x);
}

It means that y, is just another name (alias) for x within foo's scope. This is what ref means in C#.

Anzurio
That's what ref means, but it's not what reference means. I think introducing C++ into the mix confuses things, because a C++ reference isn't really the same thing as a C# reference.
Jon Skeet
I said 'reference / pointer' there than just 'reference' to highlight that I was talking about the actual reference and not the object the reference was referencing. Thought it might help save people from having to double-read the sentence.
+3  A: 

Adding "ref" might, or might not help. It all depends on the smartness of the particular marshaller implementation. If you call, for example, a web service, no amount of "ref"s is going to help you -- the parameters of the function are simply not sent back over the wire. The only thing that comes back from the service is the function's return value. When dealing with remoting you have to understand (at least to some degree) the way things actually work -- the fact that parameters need to be serialized and sent to the callee over some sort of "wire", deserialized on the other end, work is performed by the server, and the results serialized and sent back to you. Whether these results include changes to the parameters you passed in the first place depends more on the specific remoting implementation, then on the "ref"s that you add to decorate your parameters...

I'm pretty sure that both SOAP (asmx) and WCF web services will handle "ref" as you expect, by marshalling it in both directions.
Marc Gravell