473,487 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Type.IsValueType

Hello All,

I was doing a little messing around with some type conversion they
other day. Code is below.

struct ValueStruct
{
int a;

public ValueStruct(int inA)
{
a = inA;
}
}

public class Runner
{
public static void Main()
{
ValueStruct mValueStruct = new ValueStruct(10);
Console.WriteLine(mValueStruct.GetType().IsValueTy pe);

Console.WriteLine(mValueStruct.ToString());
Console.WriteLine(mValueStruct.GetType().IsValueTy pe);
}
}

My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed. This makes sense as the base implementations of these methods
are on the System.Object type.

However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.

So is there a better way to determine if an object is a value type or
a reference type? Obviously in this case the IsValueType is not really
telling me the correct information.

Thanks,
B

Aug 6 '07 #1
4 2868
On Aug 6, 1:37 pm, Brette....@gmail.com wrote:
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed.
Only if the method isn't overridden.
This makes sense as the base implementations of these methods
are on the System.Object type.
No - there's no need to box if the implementation is known at compile
time.
However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.
Indeed - and even if boxing did occur, that wouldn't change anything
about your original variable.
So is there a better way to determine if an object is a value type or
a reference type? Obviously in this case the IsValueType is not really
telling me the correct information.
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

Aug 6 '07 #2
Jon,

Thanks for the reply I visit your site almost daily so I feel like
this is a brush with fame ;-)
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed.

Only if the method isn't overridden.
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.
This makes sense as the base implementations of these methods
are on the System.Object type.

No - there's no need to box if the implementation is known at compile
time.
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.
Why do you need this information?
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
However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.

Indeed - and even if boxing did occur, that wouldn't change anything
about your original variable.
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.

On Aug 6, 7:44 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 6, 1:37 pm, Brette....@gmail.com wrote:
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed.

Only if the method isn't overridden.
This makes sense as the base implementations of these methods
are on the System.Object type.

No - there's no need to box if the implementation is known at compile
time.
However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.

Indeed - and even if boxing did occur, that wouldn't change anything
about your original variable.
So is there a better way to determine if an object is a value type or
a reference type? Obviously in this case the IsValueType is not really
telling me the correct information.

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

Aug 6 '07 #3
J2EE Convert <pi****@gmail.comwrote:
Thanks for the reply I visit your site almost daily so I feel like
this is a brush with fame ;-)
LOL - I'm really very, very ordinary.
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed.
Only if the method isn't overridden.

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.
Yes, you're right in this case. I should have read your sample code
more closely!
This makes sense as the base implementations of these methods
are on the System.Object type.
No - there's no need to box if the implementation is known at compile
time.

If the boxing is not necessary for the call to the Object.ToString()
why does it happen? I guess it just assumed that was the reason.
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.
Why do you need this information?

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 ;-)
That's almost scary... but in a good way :)

--
Jon Skeet - <sk***@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
Aug 6 '07 #4
On Aug 6, 11:26 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
J2EE Convert <pit...@gmail.comwrote:
Thanks for the reply I visit your site almost daily so I feel like
this is a brush with fame ;-)

LOL - I'm really very, very ordinary.
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed.
Only if the method isn't overridden.
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.

Yes, you're right in this case. I should have read your sample code
more closely!
This makes sense as the base implementations of these methods
are on the System.Object type.
No - there's no need to box if the implementation is known at compile
time.
If the boxing is not necessary for the call to the Object.ToString()
why does it happen? I guess it just assumed that was the reason.

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.
Why do you need this information?
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 ;-)

That's almost scary... but in a good way :)

--
Jon Skeet - <sk...@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
Thanks Jon!

Aug 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2509
by: Brian Linden | last post by:
Apparently I don't understand the IsValueType property.... I am looping through the Cache and trying to determine if the object derives from System.ValueType: Dim CacheEnum As...
10
1891
by: Qwert | last post by:
Hello, is it correct that if a type (System.Type) is a value (.IsValueType=True) and not primitive (.IsPrimitive=False), that type is a structure? Thanks.
7
4368
by: Bill Cohagan | last post by:
I need to map (at runtime) from the name of a type to the corresponding Type object so that I can access several of the attributes of that Type. Type.GetType(..) seems to require a qualified type...
5
4650
by: Narshe | last post by:
How can I check if a type is nullable? If a type is created like bool? = null;, then how can you tell if the type is nullable as opposed to not? You have to cast from bool to bool? and such, so...
4
1815
by: mark.olszowka | last post by:
I am writing a generic property table as part of my application property_name property_type property_value All information is stored in the database as strings. In my application that...
1
1769
by: Jeff Brown | last post by:
Hi, I'm trying to implement the functionality of the generic expression, "default(T)", but for a given instance of the Type class. The ideal would be if the Type class had a method like...
59
12163
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
14
34015
by: GeezerButler | last post by:
For any given type i want to know its default value. There is a neat keyword called default for doing this like object x = default(DateTime); but I have an instance of Type (called someType) and...
15
1377
by: jehugaleahsa | last post by:
Hello: I would like to get default(T). However, I don't know what T is until runtime; I just have a Type. Is there an equivilent means of getting the results of default(T)? Thanks, Travis
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.