The Best Way to ArgumentValidation
02/25/2023
Introduction to Argument Validation
In .NET7 we can do 3 different style of argument validation against null arguments. In this post I will check which one is the most performant.
The first example is the poor man's validation. We check if the input parameter is null, and if so an ArgumentNullException object is instantiated and thrown. This style of argument validation has been around for an awfully long time in .NET.
public int CustomArgumentValidationImpl(Data? input) { if (input == null) throw new ArgumentNullException(nameof(input)); return input.Value; }