tags:

views:

912

answers:

5

I have a object that is my in memory state of the program and also have some other worker functions that I pass the object to to modify the state. I have been passing it by ref to the worker functions. However I came across the following function.

byte[] received_s = new byte[2048];
IPEndPoint tmpIpEndPoint = new IPEndPoint(IPAddress.Any, UdpPort_msg);
EndPoint remoteEP = (tmpIpEndPoint);

int sz = soUdp_msg.ReceiveFrom(received_s, ref remoteEP);

It confuses me because both received_s and remoteEP are returning stuff from the function. Why does remoteEP need a ref and received_s does not?

I am also a c programmer so I am having a problem getting pointers out of my head.

Edit: It looks like that objects in C# are pointers to the object under the hood. So when you pass an object to a function you can then modify the object contents through the pointer and the only thing passed to the function is the pointer to the object so the object itself is not being copied. You use ref or out if you want to be able to switch out or create a new object in the function which is like a double pointer.

A: 

Since received_s is an array, you're passing a pointer to that array. The function manipulates that existing data in place, not changing the underlying location or pointer. The ref keyword signifies that you're passing the actual pointer to the location and updating that pointer in the outside function, so the value in the outside function will change.

E.g. the byte array is a pointer to the same memory before and after, the memory has just been updated.

The Endpoint reference is actually updating the pointer to the Endpoint in the outside function to a new instance generated inside the function.

Chris Hynes
+9  A: 

Short answer: read my article on argument passing.

Long answer: when a reference type parameter is passed by value, only the reference is passed, not a copy of the object. This is like passing a pointer (by value) in C or C++. Changes to the value of the parameter itself won't be seen by the caller, but changes in the object which the reference points to will be seen.

When a parameter (of any kind) is passed by reference, that means that any changes to the parameter are seen by the caller - changes to the parameter are changes to the variable.

The article explains all of this in more detail, of course :)

Useful answer: you almost never need to use ref/out. It's basically a way of getting another return value, and should usually be avoided precisely because it means the method's probably trying to do too much. That's not always the case (TryParse etc are the canonical examples of reasonable use of out) but using ref/out should be a relative rarity.

Jon Skeet
Good article, Jon.
itsmatt
I think you've got your short answer and long answer mixed up; that's a big article!
Outlaw Programmer
@Outlaw: Yes, but the short answer itself, the directive to read the article, is only 6 words long :)
Jon Skeet
+2  A: 

Think of a ref as meaning you are passing a pointer by reference. Not using a ref means you are passing a pointer by value.

Better yet, ignore what I just said (it's probably misleading, especially with value types) and read This MSDN page.

Brian
Actually, not true. At least the second part. Any reference type will always be passed by reference, whether you use ref or not.
Mystere Man
Actually, on further reflection, that didn't come out right. The reference is actually passed by value without the ref type. Meaning, changing the values pointed at by the reference changes the original data, but changing the reference itself does not change the original reference.
Mystere Man
A non-ref reference type is not passed by reference. A reference to the reference type is passed by value. But if you want to think of a reference as a pointer to something that is being referenced, what I said makes sense (but thinking that way may be misleading). Hence my warning.
Brian
+3  A: 

Think of a non-ref parameter as being a pointer, and a ref parameter as a double pointer. This helped me the most.

You should almost never pass values by ref. I suspect that if it wasn't for interop concerns, the .Net team would never have included it in the original specification. The OO way of dealing with most problem that ref parameters solve is to:

For multiple return values

  • Create structs that represent the multiple return values

For primitives that change in a method as the result of the method call (method has side-effects on primitive parameters)

  • Implement the method in an object as an instance method and manipulate the object's state (not the parameters) as part of the method call
  • Use the multiple return value solution and merge the return values to your state
  • Create an object that contains state that can be manipulated by a method and pass that object as the parameter, and not the primitives themselves.
Michael Meadows
+1  A: 

You could probably write an entire C# app and never pass any objects/structs by ref.

I had a professor who told me this:

The only place you'd use refs is where you either 1. Want to pass a large object (ie, the objects/struct has objects/structs inside it to multiple levels) and copying it would be expensive and 2. You are calling a Framework, Windows API or other API that requires it.

Don't do it just because you can. You can get bit in the ass by some nasty bugs if you start changing the values in a param and aren't paying attention.

I agree with his advice, and in my five plus years since school, I've never had a need for it outside calling the Framework or Windows API.

Chris
If you plan to implement "Swap", passing by ref could be helpful.
Brian