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
Printable View
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
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.PHP Code:int? i = null;
Personally I do not recommend these nulable types unless you are doing database work, they are slow (just like variants were in VB6).
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.");
}