Re: Using ref
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Wed, 2 Jul 2008 22:46:35 -0700 (PDT)
On Jul 3, 7:33 am, "Steve Harclerode" <Camel.Software...@xxxxxxxxxxxx>
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
.
- Follow-Ups:
- Re: Using ref
- From: raylopez99
- Re: Using ref
- From: Steve Harclerode
- Re: Using ref
- From: Hilton
- Re: Using ref
- References:
- Using ref
- From: Arjen
- Re: Using ref
- From: Steve Harclerode
- Re: Using ref
- From: Jon Skeet [C# MVP]
- Re: Using ref
- From: Steve Harclerode
- Using ref
- Prev by Date: Re: A bug in .Net Binary Serialization?
- Next by Date: Re: Using ref
- Previous by thread: Re: Using ref
- Next by thread: Re: Using ref
- Index(es):
Relevant Pages
|
Loading