At this moment there is no such thing as non-nullable types in C#. It is quite obvious that as any other feature it takes time and money to get it developed. No questions here. However I can see a lot of benefits in having it. One of the most obvious one is eliminating the need for null checks and leaving little-to-no chance for null reference exceptions. So assuming I am right, is there a fundamental problem that makes this feature too expensive to add to C#? If so, what is it?
-
2If the default reference isn't a null reference, what should it be? – BoltClock Nov 07 '12 at 20:35
-
How about object initialization? – Trident D'Gao Nov 07 '12 at 20:37
-
1If you have proper boundary checking, you shouldn't have null reference issues. – Maess Nov 07 '12 at 20:37
-
@bonomo: How should an object be initialized by default in C#? – BoltClock Nov 07 '12 at 20:37
-
http://programmers.stackexchange.com/ is usually a better place for these open ended questions. There is certainly merit to the idea of having non-nullable reference types, but I don't know how it compares in the C# design). Doesn't C++ always call the default constructor of the type if you just declare something? – Michael Stum Nov 07 '12 at 20:38
-
There is no need any boundary checks is the variable cannot take nulls. – Trident D'Gao Nov 07 '12 at 20:38
-
@MichaelStum In C++ all classes are value types, and so they act in just the same manor as C#'s value types in that particular respect. – Servy Nov 07 '12 at 20:42
-
@boltclock by instantiating it, that is the declaration of a non nullable variable must be always followed by initialization: String! cantTakeNull = "Anything but null"; – Trident D'Gao Nov 07 '12 at 20:43
-
@bonomo If you had that then you couldn't ever assign a `string` to it, because that string might be null. That would mean you could only ever assign another non-nullable variable to it. If you just throw an exception at runtime then it's not really different than the current behavior. – Servy Nov 07 '12 at 20:45
-
@servy, no you can, how about this: String! cantTakeNull = mayBeNull !! "Anything but null in case mayBeNull is null"; – Trident D'Gao Nov 07 '12 at 20:47
-
@bonomo Think about everything you've done so far. You've added several new operators, you've added an entirely new fundamental type, you've changed the way that the language parser needs to work as you've added a new syntax `!`. All so that you don't need to null check a variable. – Servy Nov 07 '12 at 20:52
-
Well, at the same time I am thinking of getting rid of tons of null checks throughout my code and leaving less room for stupid mistakes. All in all I think this feature is worth a shot. – Trident D'Gao Nov 07 '12 at 20:57
-
C# doesn't let you set a nullable variable type onto a non-nullable variable type or an un-initialized variable onto another variable... `Nullable
!= decimal` – Bob. Nov 07 '12 at 21:02 -
@Bob, see my answer to Servy – Trident D'Gao Nov 07 '12 at 21:05
-
Wouldn't it be better to provide a real example instead of adding your own made-up syntax/operators? – Bob. Nov 07 '12 at 21:08
-
There is no real example possible at this point because there is no such thing in C# at this point, with this said all I can do is to illustrate my point using a made-up syntax. – Trident D'Gao Nov 07 '12 at 21:12
2 Answers
Fixing the language (and the CLR) would be one thing, but the major part would be to rewrite the whole .NET Framework will all libraries. That would break all existing code expecting or checking for null. The alternative to not rewrite the libraries would give to little benefit to justify the rewrite.
It would be the same the some parts of the library today does not handle generic types.
By the way, Anders Hejlsberg has mentioned this as one of the things he regrets about the C# design.

- 46,430
- 8
- 69
- 108
-
I dont understand the part about having to rewrite the entire class library. This feature has nothing to do withthe conventional reference types. I mean there is no breaking changes by introducing it. It is up to developer whether to use it or stick with old school style with pure nullables. – Trident D'Gao Nov 07 '12 at 20:53
-
@bonomo, take a basic method, the `int.Parse(string s)`. If you have not nullable reference types in your language you would like this method to use the not nullable string type, otherwise the BCL would look half baked. – Albin Sunnanbo Nov 07 '12 at 20:58
-
Non nullables should be casted to nullables implicitly (just like value typez do) so no need for a specific signature for the Parse method – Trident D'Gao Nov 07 '12 at 21:02
-
@bonomo, but not the other way around, a nullable is not implicitly casted to a non nullable as in this case. – Albin Sunnanbo Nov 08 '12 at 10:11
In C# there exist objects by references and not. Objects that are not for references are Integers, Bytes, Floats, Booleans,... For this objects you need to set an initial value, and also they have a default value, if you don't know it you may use default()
function to get it. By other hand we have the references objects, are mostly c# objects (class), and its default value is null
.
Suppose you have a collection of an Object T, when you initialize the collection with a number of items, all items are null (its default values), having other defaults type maybe could bring other problems, for example how do you detect if that is the default value?, or when your object inherent other object, how do you define the default value (the parent, or him self)?,..., there are a lot of things that you need to define when you propose add new features to a Programming Language.
I hope this answer helps to make a bit more clear the things...

- 4,640
- 3
- 31
- 65
-
When you initialize a collection there are either no elements, or all elements are non nullable, or there is a default non nullable object, so i dont see a problem here – Trident D'Gao Nov 07 '12 at 21:09
-
If you create an array (AnyObject[10]), for example, then you need to define a default value for each "Non-nulleable" items, then how do you verify if a array item has been seated? how do you verify if is the default value? I thing there are other things that you need to define in the language before add an extra behavior. – Raúl Otaño Nov 07 '12 at 21:18
-
Answer is, you are not allowed to initialize an arrayvof non-nullables with nothing. Solution would be: providing a default non nullable value, or providing all elements of non-nullable array. – Trident D'Gao Nov 07 '12 at 21:25