This is the English version of my original post in Hungarian. Photo by Samuel Zeller on Unsplash.
The null reference is so ubiquitous, such an int...
For further actions, you may consider blocking this person and/or reporting abuse
The next version of C# will make huge strides toward making
null
a nonissue. Nullable reference types.AFAIK there are currently measures to deal with null issues in C#
They are:
Nullable types System.Nullable<
T
>Default literal
Null coalescing operator
Null conditional operator
Nullable value types introduced the concept of null where previously a value couldn’t be null. (It’s essentially an option type.) The rest of these were introduced as syntactic sugar to help us deal with the fact that null exists in the language. In upcoming versions of C# you will be able to opt into nullability. Soon, the default mode of operation in C# will be “things can’t be null”.
The benefit comes when you use Resharper because you can utilize code annotation attributes ([CanBeNull], [NotNull], [ItemCanBeNull], [ItemNotNull])
So that’s exactly what I think you’re missing here. In upcoming versions, you won’t need those annotations. Take some time to look into Nullable reference types.
I think it's far from being ready. Especailly if it introduces breaking changes. Switching midsets is difficult and if you hide a feature behind a compiler switch it's not going to work.
By the way John Skeet found 2 bugs straight away...
Awesome, I knew they were working on it but I didn't know they already have a working prototype I can download!
Very well written and straightforward - thanks!
Using the same pattern but in the context of collections, C# has ways to define "empty" collections. For example, a collection of
IEnumerable
can use the Null Object pattern this way:Looking forward to the next article!
Interesting pattern - for a more general purpose solution to the null-safety issue when no default can be defined, I'd recommend the Maybe / Option pattern, which is a universally equivalent (but type-safe) alternative to null references
Very good overview of the null object pattern.
Others already mentioned Maybe/Optional pattern. Here's my take on that pattern: github.com/j2jensen/callmemaybe
I'm definitely looking forward to nullable reference types in C# 8!
Nice post. I'm a huge fan of the "Null Object" and try to avoid any
null
in my code as good as possible. Of course, you have to considernull
as possible inputs from external sources (network, database, user input), but inside of my own code, I use "Null Object" most of the time.Well explained and easy to follow. Thank you!
You laid out the problem and then the solution very clearly. Thank you! Quite a satisfying read.
Thanks Joe!
Thank you Botond for providing a concrete example to make it easier to digest the Null Object pattern (NOP hereafter since it's too long to type 😉).
I've been using NOP happily and worked great when used in conjunction with Singleton Pattern (😱).
Shameless plug: I've touched on it briefly here.
A valid usage of Singleton Pattern (with Null object Pattern)
🕺🆂🆄🅽🅶⭐️🅺🅸🅼💃
This is awesome!
Well explained. I followed it !
Great read, Thanks!