|
-
May 13th, 2006, 08:12 AM
#1
Thread Starter
Hyperactive Member
[2.0] How to check for Null in a value type variable.
In a Value type variables like bool, int or float how to check if there is no value in that variable yet?
to check for a null in reference variable, we can check for null. Howz it possible for value types?
nath
-
May 13th, 2006, 08:30 AM
#2
Re: [2.0] How to check for Null in a value type variable.
Value types are never null.
However, in .net 2005 there are Nullable types which means that you can asign null to virtually any value type...
The "?" means that this integer is wrapped in an object that CAN be assigned null.
Personally I do not recommend these nulable types unless you are doing database work, they are slow (just like variants were in VB6).
I don't live here any more.
-
May 13th, 2006, 08:33 AM
#3
Re: [2.0] How to check for Null in a value type variable.
There's no such thing as a value type variable without a value. When you declare a variable it is created on the stack and each bit is set to zero. If the variable is a reference type then those zeroes represent null, i.e. the variable refers to no object on the heap. If the variable is a value type then those zeroes are interpreted as a value of that type. If the type is int then the value is 0, if the type is bool then the value is false, etc. If you try to test a value type for null you will get a compilation warning telling you that the test will always be false because a value type cannot be null. .NET 2.0 introduces the Nullable type to allow you to create value types variables that can be null:
Code:
Nullable<int> i = null; ;
if (!i.HasValue)
{
MessageBox.Show("i has no value.");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|