One of the main features of C # 8.0 is the concept of invalid reference types, which represents a major change in how programmers can handle null data. We discuss how they work and how to use them.
What are invalid reference types?
There are two main types of variables in C #. Value types have fixed sizes, such as
bool. They are sent between functions by value and are usually stored on the stack ̵
Nullablestruct, value types can be made to support null data if you want that behavior.
RELATED: How does memory management work in C #?
The second type of type is reference types, which are larger objects without a fixed size, such as strings and lists. They are almost always stored on the stack, with the variable on the stack as a reference to the memory location.
The problem is that reference types can become zero. The variable that stores the site can be set to a null value, which is quite common, especially when dealing with data that is not guaranteed to be there, as optional questions in a web form. This is why .NET needs a junk collector to clean up items that no longer have any active credentials.
If you are a .NET programmer, you are definitely used to it zero control, this is where you manually check if something has gone zero before using it. This works well, and it is a very cheap operation to do, but in many cases it is unnecessary. For a reference type to be zero, it must either not have been initialized with a correct value or be assigned manually to the value of
variable = null;
Zero reference types are a new addition that essentially forces a difference between reference variables that can go zero and reference variables that cannot. It’s a breaking feature that’s likely to leave your code base with many warnings, so it’s something you need to enable manually. When it’s on, the compiler starts telling the difference between:
string?, which can be zero and retains “standard” behavior from previous versions, and
string, as can not be zero. It can never be null, because it must be given a default value and can never be set to null.
With the function activated, reference types will work in much the same way as value types – never become zero unless you tell the compiler that it can with
Type? syntax. Technically, “invalid reference types” are what C # has had forever, and the new feature is the invalid reference types that replace the old ones.
This simple function allows you to inform the compiler about your intentions for the variable. If you try to assign an invalid
string? value to a non-invalid
string variable, you get a warning that you are not handling null properly.
To resolve this warning, you must set the invalid value only after checking if it is not null. The compiler is smart and is aware of when and where the value can be zero. If you insert it into one
if (value != null) block, it will not give you a warning and will remind you that it is not zero when you use it.
Unlike invalid value types, invalid reference types are implicitly converted to their non-invalid equivalents, albeit with a warning.
You can use invalid reference types wherever you can use common types, whether it is as a local variable, fields or properties for classes and structures and input parameters for functions. If you try to convert them to non-null without checking, you will get an error.
How to enable Nullable Context
From Solution Explorer, right-click on your project and select “Edit Project File.” You may need to download it first to see this option.
If you use the older project format, you may need to manually override this with a directive at the top of each file:
If you do not want to enable alerts, you can use the “Annotation” context, which only displays notes when you hover over them.