Object Parameters, they are passed as pointers, yes?

  • Thread starter Thread starter Derrick
  • Start date Start date
D

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?

Thanks!

Derrick
 
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 :)
 
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
 
Derrick said:
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.
 
Antenna said:
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.
 
Damn, You're right. 'i' is copied, not boxed. Gonna have to read that
chapter again.
Antenna said:
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.
 
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
 
Back
Top