In the previous installment of this series, we saw why the null value can be an annoying source of errors. But there are cases when you positively wish for a variable, that otherwise cannot be null, to be able to have a null value. Imagine, for example, a web site that stores the last time each user has logged in.
public class User { public string NickName { get; set; } public string Email { get; set; } public string PasswordHash { get; set; } public DateTime LastLoginTime { get; set; } } If a user has just registered but they haven’t yet logged in, we want the LastLoginTime property to be somehow empty.
I’ve found C# nullable types quite frustrating, because nullable generics don’t work properly when they can be instantiated with both value types and reference types.
It feels like a very hacky approximation of an option type, which we are now probably stuck with. Compared to all the other amazing features they’ve added, that one felt a bit half baked.
To be honest I feel the same way about it now.