0

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?

Dmitry Pavlov
  • 28,463
  • 8
  • 97
  • 113
alphacoder
  • 453
  • 1
  • 7
  • 20
  • it has ``HasValue`` property like : ``long? number = null;`` and check if it contains null like : ``if(number.HasValue)``, it will return ``true`` if it is ``null`` – Ehsan Sajjad Jul 16 '16 at 18:35

1 Answers1

3

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);

https://msdn.microsoft.com/en-us/library/72cec0e0.aspx

Josh K
  • 765
  • 3
  • 11