Using ref

This is a discussion on Using ref within the CSharp forums in Programming Languages category; Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen...

Go Back   Application Development Forum > Programming Languages > CSharp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-02-2008, 03:23 PM
Arjen
Guest
 
Default Using ref

Hi,

Form a performance perspective, is it wise to use the ref statement as much
as possible?

Thanks!
Arjen

Reply With Quote
  #2  
Old 07-02-2008, 04:22 PM
Steve Harclerode
Guest
 
Default Re: Using ref

The objects themselves are passed by reference anyway, so I would think that
using "ref" would actually slow things down a tiny bit (adds one more level
of indirection.

I only use "ref" when I need to reseat the variable to point to a different
object.

- Steve

"Arjen" <boah123@hotmail.com> wrote in message
news:1B8D9143-DA8C-4B5A-807B-DA05E6F908E0@microsoft.com...
> Hi,
>
> Form a performance perspective, is it wise to use the ref statement as
> much
> as possible?
>
> Thanks!
> Arjen



Reply With Quote
  #3  
Old 07-02-2008, 05:12 PM
Jon Skeet [C# MVP]
Guest
 
Default Re: Using ref

Steve Harclerode <Lizard.That.Was@hot.mail.com> wrote:
> The objects themselves are passed by reference anyway


No, they're not. The references are passed by value. There's a big
difference. See
http://pobox.com/~skeet/csharp/parameters.html

> I only use "ref" when I need to reseat the variable to point to a different
> object.


Exactly.

--
Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Reply With Quote
  #4  
Old 07-02-2008, 06:23 PM
Arjen
Guest
 
Default Re: Using ref


"Jon Skeet [C# MVP]" <skeet@pobox.com> schreef in bericht
news:MPG.22d5ffb147cd0b7cded@msnews.microsoft.com. ..
> Steve Harclerode <Lizard.That.Was@hot.mail.com> wrote:
>> The objects themselves are passed by reference anyway

>
> No, they're not. The references are passed by value. There's a big
> difference. See
> http://pobox.com/~skeet/csharp/parameters.html
>
>> I only use "ref" when I need to reseat the variable to point to a
>> different
>> object.

>
> Exactly.
>
> --
> Jon Skeet - <skeet@pobox.com>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon_skeet
> C# in Depth: http://csharpindepth.com


Thank you!

Reply With Quote
  #5  
Old 07-02-2008, 06:28 PM
raylopez99
Guest
 
Default Re: Using ref

On Jul 2, 2:12*pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> --


I agree with Jon Skeet here. A ref should be used when you have a
reference-type object passed to your function, which usually means
something that is instantiated with "new" in the function. But, if
you don't use "new" in your method/function for the object passed to
it, then use the default pass by value. Interestingly enough, both
pass by value and pass by reference *will* both alter the object
passed *outside* the function/method--the opposite is usually implied
in most textbook examples. In most examples it is stated that only
pass by reference will alter the object-- with usually the famous
'swap variables' example using 'temp' given--but what most people
don't realize is that this swap variable example is using 'new' inside
the function/method (check it out next time you see it, and you'll see
that's the case), which is exactly when you should use 'ref'--
otherwise stick to the default pass-by-value. If 'new' is not being
used for the object passed, use the default pass-by-value, as it's
slightly faster, as implied in this thread. The only exception to the
above is when primitive values (int, double, etc) are passed to a
function/method--but here, this seems to be an exception because these
are value-type parameters stored on the stack rather than heap, so
boxing/unboxing is involved. Technically, you could box these
primitive value-type parameters as reference-type objects, then there
would be no 'exception'. Anyway, it usually doesn't make sense to
refer to 'ref' when passing an int parameter to a method/function
anyway, so this is really not an exception to the rule.

In short, the rule is this: when using 'new' inside your method, to
instantiate a object being passed to the method (in the parameters
list), then use the 'ref' keyword in the parameters, to pass by
reference, otherwise, stick with the default (no keyword) pass-by-
value.

If the above doesn't make sense, don't worry about it--just keep
programming and it will become clear eventually.

Ray Lopez
[C# N00b MVP]
Reply With Quote
  #6  
Old 07-03-2008, 01:46 AM
Jon Skeet [C# MVP]
Guest
 
Default Re: Using ref

On Jul 3, 7:33*am, "Steve Harclerode" <Camel.Software...@hot.mail.com>
wrote:
> I've read the page, but I still don't see how my languaging would be
> considered different than "references are passed by value". What part of
> what I wrote isn't correct?


When a parameter is passed by reference, that means that changes to
the parameter are reflected in the variable used as the argument. When
a reference is passed by value, that's not the case.

So consider this code:

public void Swap (object a, object b)
{
object tmp = a;
a = b;
b = a;
}

If the parameter were passed by reference by default, that would work.
As it is, it does nothng.

The *object* isn't actually passed at all, which is another reason
that "objects are passed by reference" is incorrect. In particular,
consider the case where the reference is actually null. What's being
passed and in what way at that point?

It's a clearer and more accurate mental model to talk about references
being passed by value. It avoids confusing people who understand what
"by reference" means, and would expect the Swap method above to work
when presented with your description. (I've seen this happen on
various occasions.) It also makes it very confusing when you add the
"ref" parameter for a reference type - if things were already being
passed by reference, what would adding "ref" do?

Jon
Reply With Quote
  #7  
Old 07-03-2008, 01:47 AM
Peter Duniho
Guest
 
Default Re: Using ref

On Wed, 02 Jul 2008 23:33:57 -0700, Steve Harclerode
<Camel.Software.Co@hot.mail.com> wrote:

> I've read the page, but I still don't see how my languaging would be
> considered different than "references are passed by value". What part of
> what I wrote isn't correct?


You wrote "objects are passed by reference". But _all_ parameters,
including references to objects, are passed _by value_, except when using
"ref" and "out" of course.

The fact that the value you're passing is a reference does not make the
parameter a "by reference" parameter, nor does it mean that the object is
passed by reference.

IMHO, Jon's article does in fact explain this.

Pete
Reply With Quote
  #8  
Old 07-03-2008, 02:33 AM
Steve Harclerode
Guest
 
Default Re: Using ref

I've read the page, but I still don't see how my languaging would be
considered different than "references are passed by value". What part of
what I wrote isn't correct?

Genuinely interested,
Steve

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.22d5ffb147cd0b7cded@msnews.microsoft.com. ..
> Steve Harclerode <Lizard.That.Was@hot.mail.com> wrote:
>> The objects themselves are passed by reference anyway

>
> No, they're not. The references are passed by value. There's a big
> difference. See
> http://pobox.com/~skeet/csharp/parameters.html
>



Reply With Quote
  #9  
Old 07-03-2008, 03:36 AM
Hilton
Guest
 
Default Re: Using ref

Jon,

I have to disagree with you that objects themselves are passed by value.
Passing by value means that you pass the value; i.e. a byte[] (or Vector, or
Hashtable) being passed as value passes the entire thing - that doesn't
happen. Also, passing by value means that the thing cannot be changed;
hence passing by value not reference. But when you do "method (list)",
method can change list because a reference to the list is being passed, not
the list (aka value). C# simple passes a reference to the object which is
the same as C passing in a pointer to the data. And the method cannot
change the reference in C# just as the method in C cannot change the pointer
(from the caller's point of view) - same thing.

Pass by reference:
C# - passes a reference to the data (not the data) and the data can be
changed by the method
C - passes a pointer to the data (not the data) and the data can be changed
by the method

The example you give on your page "method (StringBuilder x)" then "x = null"
is exactly the same as C having "method (int *x)", then "x = NULL"; i.e.
pass by reference. If you view "x" as a reference to the StringBuilder
object, then you're passing the object by reference (which you said did not
happen). If you view "x" as the actual StringBuilder object, then the VM is
effectively passing &x (C terminology) - again, by reference. It uses
ldloca.s which is defined as "Loads the address of the local variable at a
specific index onto the evaluation stack, short form." - same as C.

Apart from ints, floats, etc, C# passes all objects by reference. "ref"
simply passes the address of the reference thereby allowing the method to
change it (exactly the same as C). "out" is the same as "ref" except that
the method is forced, by the compiler, to assign a value to the parameter.

How is this C code that uses pass by reference:

byte* b = {some array of bytes};
method (b);

....different to this C# code?

byte[] b = new byte[]{some array of bytes};
method (b);

In a meeting, would you ever say "Add the two string references together to
get the filename"? I wouldn't, I'd say "Add the two strings together to get
the filename". So, if we view "s" as a string, then "method (s)" is pass by
reference.

Jon, Peter, before you reply, I know that most of the literature out there
agrees with your point of view, so no need to rehash what they have already
written. Maybe it's just me, but I cannot see how C# passes objects by
value.

Hilton


Reply With Quote
  #10  
Old 07-03-2008, 03:58 AM
Steve Harclerode
Guest
 
Default Re: Using ref

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:efd1cd11-4ac9-4d62-ad01-631f589dc205@s50g2000hsb.googlegroups.com...
> public void Swap (object a, object b)
> {
> object tmp = a;
> a = b;
> b = a;
> }
>
> If the parameter were passed by reference by default, that would work.
> As it is, it does nothng.


Thinking back on my Pascal days, by golly you're right (you probably knew
that already).

> It's a clearer and more accurate mental model to talk about references
> being passed by value. It avoids confusing people who understand what


It is a better mental model, I'll use it the next time I have to mentor a
new person. God help the others I've already worked with. Actually, they
seem to be doing all right, so hopefully they absorbed the correct
information somewhere.
:-)

- Steve


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:05 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.