Boxing and implicit casting to System.Object type

  • Thread starter Thread starter Jean Stax
  • Start date Start date
J

Jean Stax

Hi !

Jeffrey Richter gives the following example in his book:

struct Point {
Int32 x, y;
}
class AAA{
....
public virtual void Add(Object value);
....
}

We want to pass the variable of Point type to add method.

Now Richter writes:
From this, you can plainly see that Add takes an Object as a
parameter, indicating that Add requires a reference (or pointer) to an
object on the managed heap as a parameter. But in the preceding code,
I’m passing p a Point value type. For this code to work, the
Point value type must be converted into a true heap-managed object and
a reference to this object
must be obtained.


My queation is: how we know from this code that Add expects reference
type ?
Is it true when I modify the code as follows ? :

public virtual void Add(System.ValueType value);

Thanks

Jean
 
Jean,
My queation is: how we know from this code that Add expects reference
type ?

Because System.Object is a reference type.

Is it true when I modify the code as follows ? :

public virtual void Add(System.ValueType value);

Yes, ValueType is also a reference type (though everything derived
from ValueType, except for Enum, is a value type).



Mattias
 
Mattias,
Yes, ValueType is also a reference type (though everything derived
from ValueType, except for Enum, is a value type).
Just for my own clarification:

Do you mean System.Enum itself, or the types that I've defined to be enums;

I've always considered types that I've defined to be enums to be value
types. I can see that System.Enum itself acts more like a reference type
just like System.ValueType.

I consider: System.TypeCode (an enum) to be a value type, while System.Enum
would be a reference type. Am I wrong?

Thanks
Jay
 
Hi Jay,
Do you mean System.Enum itself, or the types that I've defined to be enums;

System.Enum itself.

I consider: System.TypeCode (an enum) to be a value type, while System.Enum
would be a reference type. Am I wrong?

No, you're definitely right.



Mattias
 
I am still confused...
In .NET every type directly or indirectly is derived from
System.Object. According to what you are sying the System.Object is
reference type. However, System.ValueType (which is a base type for
all vlue types) is derived from System.Object (which is reference
type).
Is it not a little odd ?
 
Jean,
Does this article help?

http://www.yoda.arachsys.com/csharp/memory.html
Is it not a little odd ?
I don't think so, It has to do with the importance (ease of use) of having
every thing inherit from System.Object, plus still have actual value types.
Imagine the amount of memory used, if integers actually existed on the heap
as reference types! Can you say desperate need of the flyweight pattern
(which in itself would cause overhead).

As far as I can tell, the reason System.ValueType & System.Enum behave more
like a reference type is that when you call a routine with these two as
parameters, the value needs to be boxed as you have a variable amount of
data coming in. (byte is not the same size as long).

Hope this helps
Jay


Jean Stax said:
I am still confused...
In .NET every type directly or indirectly is derived from
System.Object. According to what you are sying the System.Object is
reference type. However, System.ValueType (which is a base type for
all vlue types) is derived from System.Object (which is reference
type).
Is it not a little odd ?

"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
Mattias,
Thanks, I was 'worried' for a second ;-)

Jay
 
Back
Top