If I have Nullable Integer variable declared and I want to know whether the Variable is Null or has some value in it.
How do I check that? Is there a function as similar to string.IsNullOrEmpty()
for Integer and Long data type?
If I have Nullable Integer variable declared and I want to know whether the Variable is Null or has some value in it.
How do I check that? Is there a function as similar to string.IsNullOrEmpty()
for Integer and Long data type?
Use the HasValue
property of the Nullable<T>
.
https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
Alternatively, you can use GetValueOrDefault
to return the contained value if the Nullable
has a value or otherwise default(T)
. For Nullable<int>
and Nullable<long>
it will be 0
, for Nullable<string>
(or any reference type), it will be null
.
You can also use GetValueOrDefault(T)
to pass a value as a default to return if HasValue
is false
. For example, the following will return 10
:
int? nullableValue = null;
int intValue = nullableValue.GetValueOrDefault(10);