Argument.Check
Guard clauses that cost nothing until they fail and explain themselves when they do.
- Guard clauses in one line — getting started with Argument.CheckFifteen lines of if/throw in front of three lines of real work, exception messages that name the wrong parameter after a rename, and a debugger that stops in the guard instead of in your code. How Argument.Check turns every guard clause into one readable line that returns the checked value, and what it does for you behind that line.
- The Throw catalogue — every check in Argument.Check, with real messagesNull checks are the easy part. The interesting guards are the ones for strings, collections, ranges, files and types, plus your own rules in one line. A tour of every Throw helper in Argument.Check, grouped by what you check, with the exact exception each one throws and the handful of details you should know before you rely on them.
- What a guard clause costs — benchmarking Argument.Check against hand-written checksA helper method for every check, isn't that slower than an if? I measured it with BenchmarkDotNet on .NET 10, comparing hand-written guards, the built-in ThrowIfNull family and Argument.Check. Most checks cost the same as an if. Two didn't, the reasons are worth knowing for any library you write, and version 5.0.8 fixed both without breaking anything.
- Argument.Check vs. ThrowIfNull and friends — when you still need a guard librarySince .NET 6, ArgumentNullException.ThrowIfNull is built in, and .NET 8 added range and whitespace guards. So why would anyone add a package for guard clauses? An honest comparison of hand-written checks, the built-in throw helpers and Argument.Check, with a decision table at the end.