Type.IsValueType - Jon Skeet [C# MVP] |
06-Aug-07 08:44:22
|
Only if the method isn't overridden.
No - there's no need to box if the implementation is known at compile
time.
Indeed - and even if boxing did occur, that wouldn't change anything
about your original variable.
Yes it is - although it doesn't always. Here's a better example of
where it doesn't:
int x = 5;
object o = x;
Console.WriteLine(o.GetType().IsValueType);
Here you really are asking a boxed value for its type - so in some
senses it should return false instead of true.
In fact, as GetType() itself isn't virtual, it *will* always (AFAIK)
involve boxing, so it would be impossible to distinguish between the
unboxed and the boxed type. I don't believe the framework libraries
allow you to distinguish between the two, I'm afraid.
Why do you need this information?
Jon |
 |
| |
Type.IsValueType - J2EE Convert |
06-Aug-07 10:23:03
|
Jon,
Thanks for the reply I visit your site almost daily so I feel like
this is a brush with fame ;-)
Yes I understand this and in my example you will see I do not override
it thus my assumption is that boxing would have happened.
If the boxing is not necessary for the call to the Object.ToString()
why does it happen? I guess I just assumed that was the reason.
General curiosity I guess. I working towards my certs and thus my
reading ends up leading to more questions then answers. Your site has
been a huge help though! I actually print out an article a day and
bring it to lunch for some reading ;-)
Thanks,
B
Are you saying that boxing did not take place? "and even if boxing did
occur" The call to ToString() at least in what I have read will result
in boxing unless overridden in the type in question. In my example I
did not provide an impl thus would it not be boxed? Sorry if I
misunderstood your answer. |
 |
| |
Type.IsValueType - Jon Skeet [C# MVP] |
06-Aug-07 12:26:42
|
LOL - I'm really very, very ordinary.
Yes, you're right in this case. I should have read your sample code
more closely!
It happens if the compiler needs to call it as a virtual call, but in
the case where the value type provides the implementation, the compiler
knows that there won't be any further overriding (due to the nature of
value types) so it can make a non-virtual call, thus avoiding boxing.
That's almost scary... but in a good way :)
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too |
 |
| |
Type.IsValueType - Brette.Ne |
06-Aug-07 12:41:20
|
Thanks Jon! |
 |
| |