By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,459 Members | 1,472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,459 IT Pros & Developers. It's quick & easy.

Object Parameters, they are passed as pointers, yes?

P: n/a
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Correct, only value types are copied.

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick

Nov 15 '05 #2

P: n/a
Consider:

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)
Derrick wrote:
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick


Nov 15 '05 #3

P: n/a
Hi Derrick,
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?


Yes, if the object is an instance of reference type (class). If the
paramter is of valuetype (struct, enum) you will have a copy of that object
for each of the classes.

But if you have parameter of type *object*
void Foo(object param)
{
}

That param might be reference to an instance of a class or boxed valuetype.
Eventhough you might share the reference to a boxed value type in the
managed heap you cannot use it for sharing data because to use the type
(read/write its properties and fields call some of its methods) you have to
unbox that valuetype and then you will have a private copy. Thus, the
changes won't go in the original.

B\rgds
100

Nov 15 '05 #4

P: n/a
Derrick <de*********@excite.com> wrote:
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?


Yes. However, you need to be aware of the difference between passing
parameters *by* reference and passing reference type parameters by
value.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information about this.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
Antenna <q1****************@hotmail.com> wrote:
Consider:

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)


You're wrong. No boxing is involved in your code, and your "ref"
parameter could just as easily be a non-ref parameter. Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
Thanks everyone!

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick

Nov 15 '05 #7

P: n/a
Damn, You're right. 'i' is copied, not boxed. Gonna have to read that
chapter again.

Jon Skeet [C# MVP] wrote:
Antenna <q1****************@hotmail.com> wrote:
Consider:

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)

You're wrong. No boxing is involved in your code, and your "ref"
parameter could just as easily be a non-ref parameter. Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.


Nov 15 '05 #8

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.

Passed by reference, huh? <big grin>

Joe
--
http://www.csharp-station.com
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.