rbwhitaker / CSharpPlayersGuideEarlyAccess

A place to track issues with the C# Player's Guide for patches and future editions
19 stars 0 forks source link

Page 175 - Confusion about null string reference #626

Closed IraGladnick closed 1 year ago

IraGladnick commented 2 years ago

The following on page 175 is both wrong, and contradicts the information about null string references on the preceding page.

"In the code above, name's type is now string?, which indicates it can contain any legitimate string instance, but it may also be null, WITHOUT THE ?, AS WE'VE DONE UNTIL NOW, WE SHOW THAT NULL IS NOT AN OPTION."

The uppercased sentence directly contradicts the preceding page, where it's explained that a regular string variable in fact can contain a null reference.

rbwhitaker commented 2 years ago

I'll take a look. Thanks for reporting this!

IraGladnick commented 2 years ago

My impression is that the text was not properly updated for the latest version of C#, and does not properly take into account whether the nullable reference type feature is enabled.

I realize this is obliquely referred to at the bottom of page 175, but as it stands the text I quoted on page 175 contradicts what appears on page 174.

rbwhitaker commented 2 years ago

I've gone back and looked at this more closely. I think it could definitely be phrased more clearly. In C# 10, having ? is supposed to indicate that null is expected, while leaving it off is supposed to indicate that null is not expected. But the compiler only gives you warnings not errors. I wouldn't be surprised if that changes to errors in future C# versions.

This sentence that you pointed out states it a bit too forcefully, saying that it cannot be done--"null is not an option." In fact, it is technically an option because you get a warning rather than an error.

I think a better phrasing on this sentence would be maybe something like:

In the code above name's type is now string?. The ? indicates it can be a legitimate string instance, but may also have no actual value--null. If you leave the ? off, you're stating that the variable is never intended to be null.

However, that has been a bit of a lie in the past. We have done things that could, on very rare occasions, actually result in a null value ending up in a variable that has indicated it isn't supposed to ever be null. You have probably noticed compiler warnings stating as much.

But these null references have just not been frequent in the past, so it hasn't been a big concern before. But now that we're making many more objects, we'll start seeing null make legitimate appearances. It is time to start paying closer attention to whether null is expected in different circumstances and cleaning up those compiler warnings.

Perhaps stated another way, the code you mention on page 174, string name = null; will technically compile, but definitely gives you a compiler warning, and isn't what you really want to do. The problem lies over there, not on page 175. But it does compile and the compiler warns you about it. (As I said earlier, I'm expecting in C# 11 or 12 that they'll eventually turn these to compiler errors instead of warnings, at which point, the offending code on page 174 won't compile anymore.)

Anyway... yeah, there are a few sentences here that could use some refinement, and I'll get them in for the next update and an erratum added to the website to provide some extra clarity. Thanks for bringing it up!