newbie question: how to test whether a variable is a value type

M

My interest

for reference type, I can use the is keyword, but how can test
whether a specific type is a value type?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Type.IsValueType

Which is
myinstance.GetType().IsValueType
 
N

Nicholas Paldino [.NET/C# MVP]

There is a problem with this (and the OP's solution) when it comes to
nullable types.

Assuming the value is in an object (if it was typed, then there would be
no reason to check for it in the first place, since you can always do
"typeof(KnownType).IsValueType"), and the value from a Nullable<T> which was
null was stored to it, you would end up with an object reference of null,
with no way of calling GetType. When boxing the nullable value, it actually
puts null (not a Nullable<T> with a value of null) in the object reference.

There really is no good way of doing this if you are using Nullable
values along with an object reference and want to call GetType on that
variable.
 
M

Marc Gravell

There really is no good way of doing this if you are using Nullable
values along with an object reference and want to call GetType on that
variable.

Absolutely true - but generics (with type inference) can help a little
bit - i.e. if you have SomeMethod<T>(T whatever) then the caller can
use SomeMethod(localTypedValue), and then if needed you can use
typeof(T).IsValueType (or whatever). Again, however, you are
completely scuppered if you get down to "object"...
I guess the point I'm trying to make is that: in addition to "object"
as a parameter, generics *also* provide a mechanism for writing a
utility method dealing with an unpredictable object-type (normally
without changing the call syntax). However, to be effective (i.e.
typeof(T) to be useful), the calling method *must* know the actual
type [even if just another "T"] (not just "System.Object").

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Marc,

I thought of that, but omitted it because if the type was known at
compile time, the whole conversation is moot, but it is a valid point
(assuming you know the type at compile time).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Gravell said:
There really is no good way of doing this if you are using Nullable
values along with an object reference and want to call GetType on that
variable.

Absolutely true - but generics (with type inference) can help a little
bit - i.e. if you have SomeMethod<T>(T whatever) then the caller can
use SomeMethod(localTypedValue), and then if needed you can use
typeof(T).IsValueType (or whatever). Again, however, you are
completely scuppered if you get down to "object"...
I guess the point I'm trying to make is that: in addition to "object"
as a parameter, generics *also* provide a mechanism for writing a
utility method dealing with an unpredictable object-type (normally
without changing the call syntax). However, to be effective (i.e.
typeof(T) to be useful), the calling method *must* know the actual
type [even if just another "T"] (not just "System.Object").

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top