ArgumentAssert 1.1.0

dotnet add package ArgumentAssert --version 1.1.0
NuGet\Install-Package ArgumentAssert -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ArgumentAssert" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ArgumentAssert --version 1.1.0
#r "nuget: ArgumentAssert, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ArgumentAssert as a Cake Addin
#addin nuget:?package=ArgumentAssert&version=1.1.0

// Install ArgumentAssert as a Cake Tool
#tool nuget:?package=ArgumentAssert&version=1.1.0

ArgumentAssert

ArgumentAssert is a library that simplifies asserting conditions on arguments. If the condition is not met, an ArgumentAssertException is thrown with a description of the failed check. In addition, the Assert() method informs code-analysis that the check must have been true if execution continues.

Run the dotnet command from your project folder to add the ArgumentAssert package:

dotnet add package ArgumentAssert

Sample: Assertion throws exception

The following code asserts the value argument is within the expected range or throws an ArgumentAssertException exception.

int AssertValueIsInRange(int value) {
    ArgumentAssertException.Assert(value is > 0 and < 10);
    return value;
}

When the assertion fails, the exception message shows the failed expression.

Unhandled exception. System.ArgumentAssertException: value is > 0 and < 10

Sample: Assertion informs code analysis

The following code asserts the value argument is not null and has a length between 1 to 9 characters. Code analysis knows that the pattern is true when Assert() returns successfully. Therefore, there is no warning for the return statement.

string AssertValueIsNotNull(string? value) {
    ArgumentAssertException.Assert(value is { Length: > 0 and < 10 });

    // no warning on the following line
    return value.ToUpper();
}

When the assertion fails, the exception message shows the failed expression.

Unhandled exception. System.ArgumentAssertException: value is { Length: > 0 and < 10 }

Sample: Streamline calls to Assert()

The following code uses the using static statement to expose the Assert() method without requiring the class name. This approach streamlines the code when many assertions are being performed throughout the codebase.

using static System.ArgumentAssertException;

int AssertValueIsInRange(int value) {
    Assert(value is > 0 and < 10);
    return value;
}

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 142 8/21/2023
1.0.0 256 2/15/2022