Why can values be null in .NET? Is this superior to having a guarantee where everything would have a value and nothing call be null?
Anyone knows what each of these methodologies are called?
Either way, I am not very knowledgeable on this, but wouldn't having a value for everything makes things easier, in terms of simplicity, i.e. eliminating null checks, and being able to write more streamlined algorithms that doesn't have to branch out for checks.
What are the pros and cons of each style in terms of performance, simplicity, parallellism, future-proofing, etc.
The reason for this is simple: null doesn't exist for value types. Why? As we have seen previously, a reference is a pointer to a memory-address that stores a value (e.g. a string, a date, a customer, whatever). If a reference points to null , then no value is associated with it.
In C#, you can assign the null value to any reference variable. The null value simply means that the variable does not refer to an object in memory. You can use it like this: Circle c = new Circle(42); Circle copy = null; // Initialized ... if (copy == null) { copy = c; // copy and c refer to the same object ... }
The problem with NULL is that it is a non-value value, a sentinel, a special case that was lumped in with everything else. Instead, we need an entity that contains information about (1) whether it contains a value and (2) the contained value, if it exists. And it should be able to “contain” any type.
We've got Tony Hoare, an early pioneer that worked on Algol to thank for that. He rather regrets it:
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
A billion is a low-ball number, I think.
UPDATE: C# version 8 and .NETCore have a decent solution for this problem, check out non-nullable reference types.
As appealing as a world without null
is, it does present a lot of difficulty for many existing patterns and constructs. For example consider the following constructs which would need major changes if null
did not exist
new object[42]
. In the existing CLR world the arrays would be filled with null
which is illegal. Array semantics would need to change quite a bit heredefault(T)
useful only when T
is a value type. Using it on reference types or unconstrained generics wouldn't be allowednull
. That wouldn't be possible in a non-null world hence fields whos type are reference types in struct's would need to be disallowedNone of the above problems are unsolvable but they do result in changes that really challenge how developers tend to think about coding. Personally I wish C# and .Net was designed with the elimination of null but unfortunately it wasn't and I imagine problems like the above had a bit to do with it.
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