C# 9 Non-Null Parameters

This neat C# 9 feature can be summed up in a tiny snippet of code. You know that code analysis warning that tells you that the argument passed might be null… wouldn’t it be nice if you could refuse to accept null?

Well, you can. This is the before…

public Book(string firstName, string lastName)

And this is the after, with bangs after the parameter name to say “this can’t be null”.

public Book(string firstName!, string lastName!)

Ideally, this would have been the default way back when, but we live and learn and can now bang-up our parameters and kill of nulls high up in our stack, if they have to exist at all.