Ok now wrap to the magic word of C#-without-null
class View
{
Model model;
public View(Model model)
{
Console.WriteLine("my model : {0}, thing : {1}", this.model, this.model.thing);
this.model = model;
}
}
What is printed on the console?
- Nothing an exception about accessing an un-initialized object is thrown : Ok call this a NullReferenceException and that's the current world.
- It doesn't build, the user needed to specify a value when declaring model, see last bullet point as it create the same result.
- Some default for the model and some default for the thing : Ok before with null we at least had a way to know if an instance was correct now the compiler is generating strange look-alikes that don't contain anything but are still invalid as model objects...
- Something defined by the type of the objects : Better as we could define a specific invalid state per object but now each object that could be invalid need to independently implement this along with a way to identify this state by the caller...
So basically for me it don't seem to solve anything to remove a null state, as the possibly invalid state still need to be managed anyway...
Oh buy the way what would be the default value of an interface ? Oh and an abstract class what would happen when a method is called on a default value that is defined in the abstract class but that call another method that is abstract ? .... .... Why oh why complicating the model for nothing, it's multiple-inheritance questions all over again !
One solution would be to change the syntax completely to go for a full functional one where the null world doesn't exits, only Maybes when you want them to exists... But it's not a C like language and the multi-paradigm-ness of .Net would be lost.
What might be missing is a null-propagating operator able to return null to model.Thing
when model is null, like model.?.Thing
Oh and for good mesure an answer to your question :
- The current class library evolved after the Microsoft-Java debacle and C# was build as a "better-Java" so changing the type system to remove null references would have been a big change. They already manage to introduce value types and removed manual boxing !
- As the value type introduction show microsoft think a lot about speed... The fact that the the default for all types map to a zero fill is really important for fast array initialization for example. Otherwise initialization of arrays of reference values would have need special threatment.
- Without null interop with C would not have been possible so at least at the MSIL level and in unsafe block they need to be allowed to survive.
- Microsoft wanted to use the framework for VB6++ removing
Nothing
as it is called in VB would have radically changed the language, it already took years for users to switch from VB6 to VB.Net such a paradigm change might have been fatal for the language.