Re: Passing arguements by reference
- From: "Mark R. Dawson" <MarkRDawson@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 26 Feb 2006 10:16:26 -0800
Hi Andrew,
I understood everything upto the code above, You say its passed by
reference, but doSomething doesnt change p unless i use "ref"? Surely
that is by value then? :-/
The subtle point to note is that the underlying object is passed by
reference, hence both p and q pointing to the same object, not to different
object BUT the actual variable which points to the object is copied when you
don't use the ref keyword.
So for example if you don't use the ref keyword then passing a reference
type is like:
void Main()
{
Test t = new Test();
t.Execute();
}
class Test
{
Person p = new Person();
Person q;
void Execute()
{
//p and q point to same object but are
//different variable
q = p;
}
void DoSomething()
{
q.Name = "bob";
//p.Name will now also be bob
q = null;
//p will not be null because q is a copy of the reference to bob.
}
}
so normally q is a parameter of the method DoSomething. When you are using
the ref keyword it is like directly accessing p inside the DoSomething
method, a copy of p is not made first (p not the thing p is pointing to).
So we have two things:
1. The underlying object being pointed to.
2. The thing that points to the object ie Person p;
When using the ref keyword on a reference type you are saying that you don't
want 2 to be be copied into the function.
In .Net classes are reference types, structs are valeu types, primities like
int, float, double etc are value types. If something derives from ValueType
then it should behave like a value type.
Mark
http://www.markdawson.org
"Andrew Bullock" wrote:
> <snip>.
Now classes are by default reference types, when you pass a reference into
a function as a parameter a copy of the object is not made, but (a subtle
point to note) the reference that refers to the object is copied if the ref
keyword is not used. For example:
void DoSomething(Person q)
{
//Change the name of the person
q.Name = "Frank";
//make q point to nothing
q = null;
}
void Main()
{
Person p = new Person();
p.Name = "Bob";
DoSomething(p);
//at this point p.Name == "Frank"
//and p is not null
}
The variables p and q were still pointing to the same object but when p was
passed to the function a copy of the p variable was made so when we set q to
null we were not setting p to null because they were different variables.
If we want q and p to be the same variable pointing to the same object we
can use the ref keyword on the ref object i.e.
void DoSomething(ref Person q)
{
q.Name = "John";
q = null;
}
void Main()
{
Person p = new Person();
p.Name = "Mark";
DoSomething(ref p);
//here p is null, because of the DoSomething method.
}
Hi, wow! Thanks alot for that really detailed answer! :-)
I understood everything upto the code above, You say its passed by
reference, but doSomething doesnt change p unless i use "ref"? Surely
that is by value then? :-/
Also, is the above true for everything by reference or just classes?
In this example:
A a1 = new A();
B b = new B(ref a1);
///
A a2;
public B(ref A aa)
{
a2 = aa;
}
Is my declaration of a2 correct? Or does it need to be some kind of
pointer, and how would i declare that?
Thanks Alot,
Andrew :-)
- Follow-Ups:
- Re: Passing arguements by reference
- From: Jon Skeet [C# MVP]
- Re: Passing arguements by reference
- References:
- Passing arguements by reference
- From: Andrew Bullock
- RE: Passing arguements by reference
- From: Mark R. Dawson
- Re: Passing arguements by reference
- From: Andrew Bullock
- Passing arguements by reference
- Prev by Date: Re: Passing arguements by reference
- Next by Date: RE: Passing arguements by reference
- Previous by thread: Re: Passing arguements by reference
- Next by thread: Re: Passing arguements by reference
- Index(es):
Relevant Pages
|
|