Hi Andrew,
in your example you are already passing the instance of A by reference
even without using the ref keyword. There are a number of ways you can pass
values:
1. Passing a value type by value
2. Passing a value type by reference
3. Passing a reference type by value
4. Passing a reference type by reference
In .Net there are two main types value types and reference types. Value
types are variables which hold the actual value of the item they refer to, so
for example:
int i = 3;
i ------- stored at ------> Memory Address 1234567 which holds value 3
so above really means i is refering to a memory location in which the value
3 is stored.
A reference type is a variable which does not hold the actual data, but
points to a memory location that stores the actual value i.e.
Person p1 = new Person("bob");
Person p2 = p2;
p1 ---stored in ------> Address 12345: contains memory address 67890
p2 ---stored in ------> Address 12134: contains memory address 67890
Memory address 67890 stores the actual data for the Person called Bob.
So even though you have two different variable they are both referencing the
same object.
When you pass value types they by default get copied, so passing an int into
a function causes the value type to be copied and it is the copy of the int
which you are accessing inside the function. If you use the ref keyword on
the int then the copy is not made and a reference to the same int is passed
into the function, for example:
using System;
using System.Text;
namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
// Value Type Examples
int j = 1;
AddOne(j);
//j will still be 1
Console.WriteLine("passing value by value: j after AddOne=" + j);
int k = 1;
AddOne(ref k);
// k will now be 2
Console.WriteLine("passing value by ref: k after AddOne=" + k);
Console.ReadLine();
}
/// <summary>
/// Passing Value type by value
/// </summary>
static void AddOne(int i)
{
i = i + 1;
}
/// <summary>
/// Passing value type by reference
/// </summary>
static void AddOne(ref int i)
{
i = i + 1;
}
}
}
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.
}
Hope that makes some sense.
Mark
http://www.markdawson.org
"Andrew Bullock" wrote:
Hi,
I have two classes, A and B,
B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);
///
A a2;
public B(A aa)
{
a2 = aa;
}
However, I figured that I could reduce memory usage and increase
performance by passing aa by reference.
How do I alter my code to do this?
I know I need to do this:
public B(ref A aa)
but how do i declare a2? If i leave it as it is, does that mean a1 and
a2 both point to the same memory location, or when assigning a2 is the
object copied?
Thanks
Andrew