0
string? nullableString

Nullable value types are understandable why they are needed. But why reference types need nullable?

Monster
  • 43
  • 4
  • 2
    Nope, they don't *need* nullable. It's an [optional feature](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references) you can activate for additional type safety. – Heinzi Oct 04 '21 at 11:14
  • 1
    They don't need it - related question asked a few hours ago - https://stackoverflow.com/questions/69433546/what-does-question-mark-after-reference-type-parameter – Ermiya Eskandary Oct 04 '21 at 11:16
  • If you are reading from a database you may need to use DBNull.Value – jdweng Oct 04 '21 at 11:25
  • 2
    @jdweng What does that have to do with anything? – mjwills Oct 04 '21 at 11:51

1 Answers1

2

Nullable references types were added to the language to decrease likelyhood of null reference exception via static code analysis by compiler:

  • Improved static flow analysis that determines if a variable may be null before dereferencing it.
  • Attributes that annotate APIs so that the flow analysis determines null-state.
  • Variable annotations that developers use to explicitly declare the intended null-state for a variable.

Nullable reference types is an opttional feature which can be enabled/disabled for example on file or project level.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132