C# 8.0 nullable operator
I converted one of our libraries to C# 8.0 and turned on the 'nullable' and 'nullable references' options.
I was pleasantly surprised to find several potential bugs where the passing of a null value would have caused a null exception, and it was simple enough to fix these. (This library was built without unit tests, horror!)
I found a few interesting things though. In the following function, we receive a warning from the compiler about a possible null dereference error.
At first I wondered if using the base string functions, instead of the above, would resolve this:
var value = "some string";
var empty = string.IsNullOrEmpty(value);
if (empty) return;
value = value.Trim(); // still getting a possible null deference
So I changed the code to the following, and the problem goes away:
var value = "some string";
if (x.IsNullOrEmpty()) return;
value = value.Trim(); // still getting a possible null deference
And then removed the function call we were using to simplify our code:
var value = "some string";
if (string.IsNullOrEmpty(value)) return;
value = value.Trim(); // error goes away
So the compiler is clever enough to pick up that the null value will cause a return, but does not know that our function will return true if given a null value. I wondered how it knew that the string.IsNull... functions would do so.
Alternatively, one can use the ! operator to tell the compiler that we know what we are doing and not to raise the warning.
var value = "some string";
if (x.IsNullOrEmpty()) return;
value = value.Trim()!; // error goes away
The real benefit of using the type? operator, aside from looking consistent, is that we get compile time warnings to prevent us having null errors at runtime. So I'm personally happy to adopt this into the future.
I was pleasantly surprised to find several potential bugs where the passing of a null value would have caused a null exception, and it was simple enough to fix these. (This library was built without unit tests, horror!)
I found a few interesting things though. In the following function, we receive a warning from the compiler about a possible null dereference error.
At first I wondered if using the base string functions, instead of the above, would resolve this:
var value = "some string";
var empty = string.IsNullOrEmpty(value);
if (empty) return;
value = value.Trim(); // still getting a possible null deference
So I changed the code to the following, and the problem goes away:
var value = "some string";
if (x.IsNullOrEmpty()) return;
value = value.Trim(); // still getting a possible null deference
And then removed the function call we were using to simplify our code:
var value = "some string";
if (string.IsNullOrEmpty(value)) return;
value = value.Trim(); // error goes away
So the compiler is clever enough to pick up that the null value will cause a return, but does not know that our function will return true if given a null value. I wondered how it knew that the string.IsNull... functions would do so.
Alternatively, one can use the ! operator to tell the compiler that we know what we are doing and not to raise the warning.
var value = "some string";
if (x.IsNullOrEmpty()) return;
value = value.Trim()!; // error goes away
The real benefit of using the type? operator, aside from looking consistent, is that we get compile time warnings to prevent us having null errors at runtime. So I'm personally happy to adopt this into the future.
Comments
Post a Comment