Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get possible null-assignment warning on nullable types

class Test {
}

//Main method
Test ccc = null;
string sss = null;

I am wondering why do I get a warning when I assign null to a string or to a class? Both are perfectly nullable. If I add the nullable ? annotation and say

Test? ccc = null;
string? sss = null;

then the warning will go away. But internally nothing has changed. What is the point of this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What is the point of this?

The point is you can try to communicate, with the absence or presence of the ? mark, if the value of the reference type can be null, or not. And the C# compiler has static analysis that tries to assist you, so you do not run into a NullReferenceException as easily as you did in older versions of C#.

If you do not like it, it is possible to set nullable context to "disable". See link Nullable reference type for more.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading