New Guard Clauses coming in .NET 8

New Guard Clauses coming in .NET 8 👇🏻 …

.NET 6 and .NET 7 introduced a couple of Guard Clauses and now .NET 8 brings a lot more. These are small but very welcome additions as they can really help clean up our code bases ❤ …

What do you think, will you use these new clauses and in what scenarios? Should we be throwing exceptions at all? Aren’t exceptions ‘expensive’ and thus shouldn’t be used for control flow?

Links

ThrowIfNullOrWhiteSpace

ArgumentOutOfRangeException

Interesting GH issue where Microsoft was considering changing the APIs of the new .NET 8 Guard Clauses to actually return the input being validated. It looks like they’ve decided against changing things at this stage so the clauses will return void -> [API Proposal]: Consider changing return types of new throw helpers · Issue #86341 · dotnet/runtime (github.com)

Leave a Reply

Your email address will not be published. Required fields are marked *