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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With