Re: Passing arguements by reference
- From: "Bill Butler" <qwerty@xxxxxxxx>
- Date: Sun, 26 Feb 2006 16:47:15 GMT
"Andrew Bullock" <andrewREMOVEbullockTHIS@xxxxxxxxxxxxxxxxxxx> wrote in message
news:oIkMf.18500$gB4.17084@xxxxxxxxxxxxxxxxxxxxxxx
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.
That would be an incorrect assumption.
In the code above, you are already passing a reference to an A object in to the constructor for B.
In C#, you never directly pass objects as parameter, instead you pass references to objects.
Thus, in class B, you only pay for the cost of a reference.
The net effect is that a1 and a2 in your code both are references to the same A object.
If you don't wish to share the object, then you need to clone it and save the reference to the
clone.
Of course, since you mentioned reducing memory usage, this is obviously not a problem in this case
Hope this helps
Bill
<snip>
.
- Follow-Ups:
- Re: Passing arguements by reference
- From: Andrew Bullock
- Re: Passing arguements by reference
- References:
- Passing arguements by reference
- From: Andrew Bullock
- Passing arguements by reference
- Prev by Date: Re: Conversion signed/unsigned
- Next by Date: Adding an icon to Listview items
- Previous by thread: Passing arguements by reference
- Next by thread: Re: Passing arguements by reference
- Index(es):
Relevant Pages
|