Has anyone played with this ??
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/diagnostics/guardThe CommunityToolkit.Diagnostics package contains APIs to efficiently validate method parameters and to throw exceptions in faulting code paths. It is meant to be used to help simplify all argument checks and to make them more expressive and easier to read, while at the same time improving codegen quality and performance.
This package can be installed through NuGet, and it has the following multi-targets:
.NET Standard 2.0
.NET Standard 2.1
.NET 6
This means the package can be used on any available runtime (including .NET Framework, .NET Core, UWP, Unity, Xamarin, Uno, Blazor, etc.).
The Guard can be used to validate method arguments in a streamlined manner, which is also faster, less verbose, more expressive and less error prone than manually writing checks and throwing exceptions.
>>>> . .
public static void SampleMethod(int[] array, int index, Span<int> span, string text)
{
Guard.IsNotNull(array);
Guard.HasSizeGreaterThanOrEqualTo(array, 10);
Guard.IsInRangeFor(index, array);
Guard.HasSizeLessThanOrEqualTo(array, span);
Guard.IsNotNullOrEmpty(text);
}
Prior to C#10 the syntax is :
Guard.IsNotNull(array, nameof(array)))).
//instead of :
Guard.IsNotNull(array);
It's basically a ready built specific assert API with a supporting
'ThrowHelper' class
I haven't had a play yet.
Looks interesting, to my mind.