Intoduction
What are guard clauses ? Guard clauses are true/false expressions (predicates) found at the top of a method or function that determine whether the function should continue to run. Guard clauses test for preconditions and either immediately return from the method or throw an exception, preventing the remaining body of code from executing.
Example
A common example is to check if an argument is null
and throw an ArgumentNullException
. However, one may be performing other forms of validation on arguments such as if dates fall within a certain date range, integer values are positive, strings are a certain length, etc. Any assumptions about the arguments or state of the object that are being made by the remaining block of code are typically made explicit by the guard clauses.
void Add(User user) { // Guard clause if (user == null) throw new ArgumentNullException(nameof(user)); // rest of the code... }
What is Fluent Guard?
Fluent Guard is a simple library that helps you to use Microsoft.Toolkit.Diagnostics.Guard extensions with more fluency.
Logo

The idea is not new, I tried to copy some code and personalize some code from the project FluentAssertions.
Installation
You can download and install the package manually from Nuget org:
Or run this command on the package manager of your Visual studio:
Install-Package Mahdhi.GuardFluently.Core
Usage
Let’s say we have a service SomeService
that defince the following method:
public void DoSomeCall(string name, object length) { // do something }
Here you will sure need to make some checks for your parameters name
and length
.
Let’s say :
- the
name
shouldn’t be null and should have at least 10 chars. - the
length
should be assignable to the typeint
In this case your check can look like this,
public void DoSomeCall(string name, object length) { //guard name.Should() .NotBeNullOrWhiteSpace() .And .HaveLengthGreaterThan(10); length.Should().BeAssignableTo(typeof(int)); // do something }
In this example there are three exceptions that can be thrown:
System.ArgumentNullException
: Thrown ifname
is null.System.ArgumentException
: Thrown ifname
is empty or whitespace.System.ArgumentException
: Thrown iflength
is not assignable to the typeint
.
If you call this method as following:
SomeService someService = new(); try { someService.DoSomeCall("A", 7); Console.WriteLine("No exception..."); } catch (Exception ex) { Console.WriteLine(ex.Message); }
Here the name ‘A’ is shorter than 10 letters, in this case an exception will be thrown:

But with this example, where name and length are valid:
try { someService.DoSomeCall("My size is over that 10", 7); Console.WriteLine("No exception..."); } catch (Exception ex) { Console.WriteLine(ex.Message); }
In this case no exception will be thrown.
I hope this blog could give you some new infos 🙂
References
David Hayden – Guard Clauses in Computer Science
online us pharmacy
safe online pharmacy
online pharmacy canada
pharmacy schools online
gabapentin 800 mg pill
reputable online pharmacies
pharmacy technician certification online
buy cialis from canada
Instalar La Propecia cialis from usa pharmacy
international online pharmacy
tadacip price
brand cialis online canada
azithromycin 500 buy online
can you buy Movfor over the counter
erythromycin ophthalmic https://zithromax.beauty
order Molnupiravir with codeine
Real Viagra For Sale Online 178 buy viagra cialis online
buy Molnupiravir uk
cheapest sildenafil 100mg uk
buy amoxicillin canada
Hydrochlorothiazide Kidney Disease buy cialis uk
where can i buy Movfor syrup
Canadian Pharmacies That Sell Clomid buy cialis cheap
order Molnupiravir online
can you buy Molnupiravir over the counter
Movfor medicine to buy
buy stromectol 12mg sale
prescription drug price
canadadrugstore com
can i buy cialis in canada over the counter
generic drugs india
phenergan 100mg
buy avodart
compare drug costs
ivermectin tablets https://stromectolgl.com stromectol for humans
drugstoreonline
online doctor for ed meds
ed drug costs in canada
https://stromectolhub.com/ ivermectin over the counter
ed meds online canada
самоходный подъемник
https://podyemniki-machtovyye-teleskopicheskiye.ru
rate canadian pharmacies
azithromycin tablet price
buy tadacip 20 mg without prescription
online prescription pharmacy
ed pills online for sale
your pharmacy online
are online ed pills safe
canadian drugstrorecom
ed pills online