ref function parameter question

  • Thread starter Thread starter Glenn Lerner
  • Start date Start date
G

Glenn Lerner

If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy). So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)

If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar to double pointer in C?

I tried to find this out while debugging but I wasn't able to display
the address of variable from watch window (similar to VC++ debugger
displaying the actual address).

Please advise,
Glenn
 
Glenn Lerner said:
If I pass a reference type (such as DataSet) to a function, I'm assuming
only a reference is passed (not a copy).
Correct.

So there is no need to declare
function parameter as ref for those types?
Example: private void myFunction(ref DataSet data)

Not if you're happy with pass-by-value semantics of the reference.
Passing the parameter by reference would change the semantics if you
changed the value of the formal parameter within the method.
If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar to double pointer in C?

It will pass the parameter *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html
 
Not if you're happy with pass-by-value semantics of the reference.
Passing the parameter by reference would change the semantics if you
changed the value of the formal parameter within the method.


It will pass the parameter *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

In other words, it's passing a reference to the parameter, which is a
reference to an object, and if the method changes the reference to
point to another object, the reference used as an actual parameter in
the calling code will also refer to a different object.

It is similar to a double pointer in C, but just a lot more automagic.
 
Back
Top