Null handling
When the underlying primitive is a reference type, such as string, null is never a valid value. How it is treated depends on the method you use to create or parse an instance.
Instantiation
Passing null to From(...) will lead to ArgumentNullException:
var username = Username.From(null!); // throws ArgumentNullExceptionAt the same time, TryFrom(...) will not throw on null and instead will return false:
Username.TryFrom(null!, out var username); // returns falseParsing
Parsing follows the same rules. Parse(...) parses the input into the primitive and then validates it, so a null input leads to ArgumentNullException:
var username = Username.Parse(null!); // throws ArgumentNullExceptionTryParse(...) will not throw on null and instead will return false:
Username.TryParse(null, out var username); // returns falseNull propagation
Sometimes you might want to treat null as "no value" instead of invalid input and propagate it through. In such case you need to use FromNullable(...) instead of From(...):
Username? username = Username.FromNullable(null); // returns nullTryFromNullable(...) does the same for the Try pattern:
Username.TryFromNullable(null, out var username); // returns true, username is null