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
To denote the nothingness concept, since 0 is not the right fit.
Now you can give any value type a Null value by defining a nullable type.
I think we can't have a value always for the variable because at first we have to default it to some value, and here comes the question why a specific value takes advantage over the others.
Many people probably can't wrap their head around coding without nulls, and if C# didn't have nulls, I doubt it would have caught on to the extent that it has.
That being said, a nice alternative would be, if you want to allow a nullable reference, then the reference would have to be explicitly nullable, like with value types.
For example,
Person? person = SearchForPersonByFirstName("Steve");
if (person.HasValue)
{
Console.WriteLine("Hi, " + person.Value.FullName);
}
Unfortunately, when C# 1.0 came out, there was no concept of Nullable; that was added in C# 2.0. Forcing references to have values would have broken the older programs.
Well, values (value-type vars) can only be null
since the nullable types were introduced in Fx2.
But I suppose you mean:
null
?That is part of the usefulness of references. Consider a Tree or LinkedList, they would not be possible (unable to end) without null
.
You could come up with many more examples, but mainly null
exists to model the concept of 'optional' properties/relationships.
null
is just the name of the default value for a reference type. If null
was not allowed, then the concept of "doesn't have a value" wouldn't go away, you would just represent it in a different way. In addition to having a special name, this default value also has special semantics in the event that it is misused - i.e. if you treat it like there is a value, when in fact there is not.
If there were no null
:
In summary:
The problems with null
described by Tony Hoare are typically due to the fact that prior to modern virtual machines, runtime systems did not have nearly as clean handling of misused null
values like you have today. Misusing pointers/references does remain a problem, but tracking down the problem when working with .NET or Java tends to be much easier than it used to be in e.g. C.
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?
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 :
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.