-2

Are there other scenarios where I need to use one of the following other than what is mentioned?

  • Nullable.GetValueOrDefault(): If the value is null, you will get null, otherwise the value.
  • Nullable.Value: If the value is null, an exception will be thrown.
  • Nullable<>.HasValue: Returns true or false.
  • Nullable<> != null Same as above.

I was a bit confused because I have seen codes on GitHub check the HasValue or != null then return the value if true, otherwise return null. I would rather just use the first. Is there any side-effect or something that I am missing?

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99

1 Answers1

3

Nullable.Value throws an exception if Nullable.HasValue is false. Nullable.GetValueOrDefault() returns the type's default value if Nullable.HasValue is false.

glenebob
  • 1,943
  • 11
  • 11