ref vs out

  • Thread starter Thread starter C# newcomer
  • Start date Start date
C

C# newcomer

Hi friends,
Is there any advantage or disadvantage (good or bad
practice) in using a ref argument vs an out argument or
vice versa? Thanks for helping me better understand.
 
a ref argument must be constructed by the caller and passed to the method,
thus requiring extra bandwidth to send the object
both ways.

an out argument is created by the method and returned to the caller, thus
requiring only one trip (instead of the two for a ref
argument).
 
Ken Onweller (.NET MCSD) said:
a ref argument must be constructed by the caller and passed to the method,
thus requiring extra bandwidth to send the object
both ways.

an out argument is created by the method and returned to the caller, thus
requiring only one trip (instead of the two for a ref
argument).

Actually, I suspect the amount of data that is passed is the same in
both cases - it's the location of the variable in question. After that
location has been passed, I don't see why there would be any difference
in code (by the time it's been compiled to IL in the first place; there
are different restrictions about what can be done with it.) I don't
think there's likely to be any significant performance benefit in using
one over the other.

However, there is a *semantic* difference between them, and that's what
should be considered when designing an interface (public or private).
 
Jon Skeet said:
Actually, I suspect the amount of data that is passed is the same in
both cases - it's the location of the variable in question. After that
location has been passed, I don't see why there would be any difference
in code (by the time it's been compiled to IL in the first place; there
are different restrictions about what can be done with it.) I don't
think there's likely to be any significant performance benefit in using
one over the other.

That would only be the case if the two objects are running in the same
thread or have to written to support safe access from multiple threads. If
any marshalling is required, the ref argument would needlessly marshal the
data before calling the function. This could be a serious performance
problem if the marshalling is into another process or another computer.

Regards,
Aaron Queenan.
 
Thanks for your reply Jon. Can you explain to me what you
meant by the "semantic" difference and why you mentioned
about the "public" and "private" scope here. Please bear
with my ignorance.
 
Aaron did a better job of explaining it than me. I've been dealing a lot
with Marshalling lately so this has
been heavily on my mind. sending a ref thru a webservice is a lot more
costly than having an out.
 
C# newcomer said:
Thanks for your reply Jon. Can you explain to me what you
meant by the "semantic" difference and why you mentioned
about the "public" and "private" scope here. Please bear
with my ignorance.

The semantic difference is in terms of "Is the original value of the
parameter used?" If so, you should use a ref parameter. If not, then
it's only the *final* value which is important, and you should use out.

(For preference, you shouldn't use either IMO unless you *really* need
it, but there we go.)

The public/private business was just stressing that similar care should
be taken over designing the private interface within your class as the
public interface your class exposes.
 
Aaron Queenan said:
That would only be the case if the two objects are running in the same
thread or have to written to support safe access from multiple threads. If
any marshalling is required, the ref argument would needlessly marshal the
data before calling the function. This could be a serious performance
problem if the marshalling is into another process or another computer.

Yes, marshalling would indeed make a big difference - something I
hadn't thought about. (Not sure what you mean about the two "objects"
running in the same thread though - objects don't run in threads...)

I haven't looked at how remoting works under the covers, to be honest,
and in particular how it handles ref/out parameters. Personally I try
to avoid ref/out parameters anyway, on the general principle that
there's usually a better design that doesn't require them. They're
handy every so often though.
 
Jon Skeet said:
Yes, marshalling would indeed make a big difference - something I
hadn't thought about. (Not sure what you mean about the two "objects"
running in the same thread though - objects don't run in threads...)

I probably didn't phrase that bit too well. :-( By "running in the same
thread" I meant being created in the same STA appartment, so the same thread
will always be used to access them, direct memory access is okay, and
therefore marshalling isn't required.

Aaron.
 
Back
Top