Biggest mistake in C#: That strings can be null
I really like C#. It could be better by adding lots of my favorite things … but as it stands it is very useable, very expressive, very readable. And it has only one major mistake (IMO): Strings (variables, parameters, fields, etc.) can be null.
Oh my, how many coding errors have been made by forgetting strings could be null? How many crashes have users suffered? Oh well.
Anyway, here’s a brief proposal on how to correct the problem. It isn’t carefully thought through … just off the cuff as it were. But:
Let there be a unary operator that, when applied to a typed null value (something that isn’t dynamic) acts like this: if the value is not null then the operator returns that value unchanged; if the value is null then the operator returns the result of calling the no-parameter constructor for the type of the value. (Where the type of the value is whatever the compiler things it is using standard type inference, where it’s an error if the type doesn’t have a no-parameter constructor, etc.)
(For the sake of argument, assume the operator symbol is a postfixed exclamation point.)
Then you could easily (single character!) coerce any null value to a default constructed value of the proper type. It would be easy to insert the operator at return statements, or after a method call where you weren’t sure if a null value might be returned, or on the use of a parameter.
public string Append(this string s; string e)
{
return s! + e!;
}
And then, the next step is to allow that operator to be used in three more places: After the declared return type of a method, after the declared parameter type of any method parameter, and after the declared type of a property. (It would work with generic parameter and return types too, if the generic type had the new() constraint.) This annotation would mean that the compiler would automatically apply the operator at each return statement, and on each annotated parameter on method entry. The annotation would also be a simple and easily understood way to communicate to the programmer the guarantee that the method never returned null and that, inside the method, the parameters would never be null.
public string! Append(this string! s; string! e)
{
return s + e;
}
And, with those annotations in place, if you went ahead and modified the IL to incorporate the annotations (rather than just having it as a C# compiler implemented feature) then the JITter could perform flow analysis (of whatever complexity) and probably eliminate a bunch of explicit invocations of the operator.
The final step: Annotate the .NET framework with the operator where appropriate, which would be practically everywhere.
public class List<T> : ...
{
...
public T! Find(Predicate<T> match) { ... }
...
}
public static class Path
{
...
public static string! GetFileName(string! path) { ... }
...
}
Well … your input? Good idea, I’ve neglected a major flaw, or what?