Brief Hints: C# Nullable Types and Arrays, Special Double Values
Brief Hints: I wanted to show you three things in C# that I’ve been using a lot lately.
Nullable Types are a convenient language construction in C# that allows one to assign a primitive type with null…
double? someval = null; // declares a nullable double called 'someval'.
The question mark suffixing the keyword double makes the variable someval nullable. This was originally designed so that one can retrieve values from LINQ to SQL without checking for nulls (SQL inherently makes this distinction). This could be thought of as yet another construct to make autoboxing primitives more intuitive and more entrenched in the language.
I use nullable types when I need a special ‘unassigned’ value for “find the greatest” or “find the least” kinds of loops.
When we apply Nullable Types to Arrays, we get an array of nullable primitives (arrays are already nullable, being first class objects).
double?[] somevals = new double?[10]; // declares an array called 'somevals' of ten nullable doubles.
An array of primitives is initialized with all values 0.0; whereas an array of primitve?s is initialized with nulls.
Special Double Values are also something that I’ve started using a lot. There are many algorithms I’ve been coming across that use “magic numbers” corresponding to arbitrarily high and arbitrarily low numbers. Instead of using evil magical quantities, I’ve been using Double.PositiveInfinity and Double.NegativeInfinity. C# makes it easy to assign three more special quantities: Double.NaN, Double.MinValue and Double.MaxValue.
Edit: I forgot to mention why I kept italicizing the word primitive. C# doesn’t really have primitives that are exposed to the developer– everything actually IS an object, and the illusion of boxing or not isn’t really relevant. This just makes nullable types all the more logical.
1) The PositiveInfinity and friends are available in pretty much every language I’ve seen.
2) Why do Java and C# insist that all non-primitive types must be nullable? That is, why can’t I declare an object that must be non-null. There are many situations where this would be convenient.
3) The above generalises quite nicely to array then. I should be able to specify whether the array itself is non-null or the array can be null but the contents cannot (default).
Null gets very special treatment in object-oriented land and I find this increasingly annoying. [end of mini-rant]
I was thinking of (2) today… I suppose that some syntactic sugar would be used to specify that some variable should NOT be nullable– maybe a suffix that does exactly the opposite of what I’ve discussed above.
An ad hoc solution would inevitably take the form of some terrible factory design pattern. Eww.
I can’t immediately think of a useful application for (2) to be honest– I’m actually happier when everything is nullable. It sits better with me… I bet that this is more than slightly influenced by how much I take advantage of the ridiculously late binding in Python where you can just catch NameErrors for not having declared everything you need beforehand. Yes, it’s evil. 😛
I think your conclusion above actually isn’t really about “null” at all, but the treatment of first-class objects vs. the treatment of (emulated) primitives.