🌀 Dealing with Nothing in C# - Option

It is a very common pattern to return null when something does not exist. Suppose we have a system where a UserRepository object can retrieve User objects from persistent storage - like a database. public class UserRepository { public User Get(string email) => context.Users.SingleOrDefault(u => u.Email == email); /* ... */ } This is also a very common source of bugs. Consumers of the UserRepository routinely forget to check whether the reference returned is null, causing the code to blow up at runtime unexpectedly....

December 15, 2016 · 9 min · 1844 words · Botond