-4

Should I use the Nullable type whenever I do extensive testing to see if my object is going to be null?

For example, is it better to do a check by using "Object == null" or by creating a Nullable type and then checking Object.hasValue?

Essentially, why are Nullables preferred if I can just do a check "Object == null"? I understand where this might be the case for simple types but not Complex Types?

SamIAm
  • 2,241
  • 6
  • 32
  • 51
  • 7
    You can use `Nullable` only with value types, not with reference types. `Nullable` violates the generic constraint `T : struct`. Value types cannot be null, but can be wrapped in a Nullable to allow just that: `Nullable i = null` – knittl Mar 10 '15 at 10:42

1 Answers1

0

From Using Nullable Types (C# Programming Guide)

For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies "undefined". In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set to null to indicate that they are not initialized.

MRebai
  • 5,344
  • 3
  • 33
  • 52