Yusif
Yusif

Reputation: 551

What is the point of making a reference type nullable?

The documentation for nullable reference types says:

The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type. The compiler doesn't add any runtime checking for non-nullable reference types. The benefits are in the compile-time analysis. The compiler generates warnings that help you find and fix potential null errors in your code. You declare your intent, and the compiler warns you when your code violates that intent.

What are the potential null errors? What intent do I declare using a nullable reference type? This is not clear to me.

Upvotes: 3

Views: 4119

Answers (4)

DIMMACK
DIMMACK

Reputation: 197

It's useful when you disable nullable on a project file (<Nullable>disable</Nullable> or <Nullable>annotations</Nullable>) and you have to explicitely mark a reference as nullable.

Upvotes: 0

Mario
Mario

Reputation: 11

It is just a notation to tell the compiler that this object can be null and trigger warnings. Actually, it means nothing to the final compiled code. You can either write question marks everywhere to shut down warnings, ignore them, or disable nullable in the project file and work as references can always be null, as we have being always working. Contrary to believe, a reference market as non nullable can be perfectly null. I suppose they didn't use the C notation MyClass* because C# was designed to get rid of that and they use MyClass?

Upvotes: 0

gsharp
gsharp

Reputation: 27927

Maybe some sample code helps you to understand better:

string? myString1 = RemoveDots1("Hel.lo");
string myString2 = RemoveDots1("Hel.lo"); // warning: the function might return null, but you declared myString2 as non nullable.


myString1 = null;
myString2 = null; // warning: you should not do that, because you declared myString2 as non nullable.

myString1 = RemoveDots1(myString1);
myString1 = RemoveDots2(myString1); // warning: myString1 could be null, but its expecting a non nullable string


string? RemoveDots1(string? myString)
{
    return myString.Replace(".", ""); // warning: myString could be null
}


string RemoveDots2(string myString)
{
    return myString.Replace(".", "");
}

Upvotes: 4

jmcilhinney
jmcilhinney

Reputation: 54427

Originally, all reference-type variables were nullable. This meant that you always had to consider the possibility of a null reference and code accordingly. If it was your intention that a variable never be null, there was no way to enforce that. If you treated it like it would never be null, there was always the possibility of a NullReferenceException at run time if someone mistakenly set it to null.

Now, you can specify which variables should be nullable and which shouldn't and the compiler will enforce that. If you try to make a non-nullable variable null, a compiler error will prevent that. If you treat a nullable variable as though it definitely won't be null, a compiler warning will alert you.

Upvotes: 8

Related Questions