It depends on what you prefer.
The most common code is similar to the C # you are showing:
if (bar == null) throw new ArgumentError("arg is null");
It provides useful error messages and prevents the execution of incorrect code (for example, accidentally formatting hard drives at level zero).
I would write:
Foo(Bar bar) : this.bar = bar { if (bar == null) throw new ArgumentError(...); }
because I find it more readable than alternatives, but if you prefer, you can even write:
Foo(Bar bar) : this.bar = (bar == null) ? throw new ArgumentError(...) : bar;
Using "assert (bar! = Null)" works great. It only catches the problem in checked mode, but if it's just to protect itself (for example, from the inner class in your library), that's good enough. For public functions and classes, I would prefer if-throw.
source share