Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
My interest - 02 Jan 2008 20:34 GMT
for reference type, I can use  the is keyword, but how can test
whether a specific type is a value type?
My interest - 02 Jan 2008 20:37 GMT
Sorry for the stupid question, the answer is is ValueType
Ignacio Machin ( .NET/ C# MVP ) - 02 Jan 2008 20:47 GMT
Hi,

Type.IsValueType

Which is
myinstance.GetType().IsValueType

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

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

Nicholas Paldino [.NET/C# MVP] - 02 Jan 2008 22:02 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>
[quoted text clipped - 5 lines]
>> for reference type, I can use  the is keyword, but how can test
>> whether a specific type is a value type?
Marc Gravell - 02 Jan 2008 22:51 GMT
> 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
Nicholas Paldino [.NET/C# MVP] - 03 Jan 2008 15:25 GMT
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).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> 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
[quoted text clipped - 13 lines]
>
> Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.