Why does null exist in .NET?

前端 未结 11 1568
醉酒成梦
醉酒成梦 2020-12-24 06:04

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

11条回答
  •  心在旅途
    2020-12-24 06:39

    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.

提交回复
热议问题