How Do C#'s Nullable Reference Types Work
How Do C#'s Nullable Reference Types Work
We select and review products independently. When you purchase through our links we may earn a commission. Learn more.
How-To Geek
🏡 DevOps Programming
https://www.howtogeek.com/devops/how-do-cs-nullable-reference-types-work/ 1/5
5/26/22, 2:04 AM How Do C#’s Nullable Reference Types Work?
The other kind of type is reference types, which are larger objects
without a fixed size, like strings and lists. They are almost always
stored on the heap, with the variable on the stack being a reference
to the memory location.
Problem is, reference types can go null. The variable that stores the
location can be set to a null value, which is pretty common especially
when dealing with data that isn’t guaranteed to be there, like optional
questions in a web form. This is why .NET needs a garbage collector,
to clean up objects that no longer have any active references.
variable = null;
ADVERTISEMENT
Seamless | 31 May-1
Jun | Free
Seamless Middle East
https://www.howtogeek.com/devops/how-do-cs-nullable-reference-types-work/ 2/5
5/26/22, 2:04 AM How Do C#’s Nullable Reference Types Work?
With the feature enabled, reference types will work in largely the
same way as value types—never going null unless you tell the
compiler it can with the Type? syntax. Technically, “nullable reference
types” are what C# has had forever, and the new feature is the non-
nullable reference types that replace the old ones.
This simple feature allows you to inform the compiler about your
intentions for the variable. If you try to assign a
nullable string? value to a non-nullable string variable, you’ll get a
warning that you’re not handling null properly.
To fix this warning, you’ll need to set the non-nullable value only after
checking if it’s not null. The compiler is smart, and is aware of when
and where the value may be null. If you wrap it in a if (value !=
null) block, it won’t give you an warning, and will remind you that it’s
not null when using it.
https://www.howtogeek.com/devops/how-do-cs-nullable-reference-types-work/ 3/5
5/26/22, 2:04 AM How Do C#’s Nullable Reference Types Work?
You can use nullable reference types anywhere you can use regular
types, whether that’s as a local variable, fields, or properties for
classes and structs, and input parameters for functions. If you try to
convert them to non-null without checking, you’ll get an error.
<Nullable>enable</Nullable>
ADVERTISEMENT
https://www.howtogeek.com/devops/how-do-cs-nullable-reference-types-work/ 4/5
5/26/22, 2:04 AM How Do C#’s Nullable Reference Types Work?
rano
Sonar
ranoS
The Home of
Clean Code
Open
If you are using the legacy project format, you might need to
manually override this with a directive at the top of each file:
#nullable enable
If you don’t want to enable warnings, you can use the “Annotation”
context, which will only show annotations when you hover over them.
ANTHONY HEDDINGS
Anthony Heddings is the resident cloud engineer
for LifeSavvy Media, a technical writer, programmer,
and an expert at Amazon's AWS platform. He's
written hundreds of articles for How-To Geek and
CloudSavvy IT that have been read millions of times. READ FULL BIO »
How-To Geek is where you turn when you want experts to explain technology. Since we launched in
2006, our articles have been read more than 1 billion times. Want to know more?
https://www.howtogeek.com/devops/how-do-cs-nullable-reference-types-work/ 5/5