One of the key features of C # 8.0 is the concept of nullable reference types, which is a major shift in how programmers handle null data. We will discuss how they work and how to use them.
What are nullable reference types?
There are two main types of variables in C #. Value types have fixed sizes, such as
bool. They are passed by value between functions and are usually stored on the stack ̵
NullableStructure, value types can be created to support null data if you want this behavior.
CONNECTED: How does memory management work in C #?
The other kind of type are reference types, which are larger objects without a fixed size, such as B. Strings and Lists. They are almost always stored on the heap, with the variable on the stack being a reference to the location.
The problem is that reference types can become null. The variable that stores the location can be set to a null value. This is particularly common when processing data whose existence is not guaranteed, e.g. B. Optional questions on a web form. For this reason, .NET needs a garbage collector to clean up objects that no longer have active references.
If you’re a .NET programmer, you’re definitely used to it Zero checkHere you manually check to see if anything has gone null before using it. This works well and is very cheap, but in many cases unnecessary. For a reference type to be null, it either does not have to be initialized with a suitable value or has to be assigned the value of manually
variable = null;
Nullable reference types are a new addition that essentially enforces a difference between reference variables that can become null and reference variables that cannot become null. It’s a breaking feature that will likely leave your codebase with lots of warnings, so you’ll have to enable it manually. Once it’s on, the compiler will tell the difference between:
string?, which can be null and keep the “default” behavior of earlier versions, and
string, Which can not be zero. It can never be null because it must be assigned a default value, and it can never be set to null.
When enabled, reference types work much the same as value types. They never become null unless you tell the compiler that it can
Type? Syntax. Technically, “nullable reference types” are what C # has always had, and the new feature is the non-nullable reference types that replace the old ones.
You can use this simple function to inform the compiler of your intentions for the variable. When you try to assign a null value assignment
string? Value to a non-nullable value
string Variable will give you a warning that they are not handling null properly.
To resolve this warning, you must set the non-resettable value only after checking that it is not null. The compiler is intelligent and knows when and where the value can be null. When you wrap it in one
if (value != null) Block, it doesn’t give you a warning and remind you that it’s non-null when you use it.
Unlike nullable value types, nullable reference types are implicitly converted to their non-nullable equivalents, albeit with a warning.
You can use nullable reference types wherever you can use regular types, regardless of whether they are local variables, fields, or properties for classes and structures, or input parameters for functions. If you try to convert it to non-zero without checking, you will get an error message.
How to activate the nullable context
In Solution Explorer, right-click your project and choose Edit Project File. You may have to unload it first to see this option.
If you are using the legacy project format, you may need to manually override this with a directive at the beginning of each file:
If you don’t want to enable warnings, you can use the Annotation context, which only displays annotations when you hover over them.