SafeAgainst 8.0.0

dotnet add package SafeAgainst --version 8.0.0
NuGet\Install-Package SafeAgainst -Version 8.0.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="SafeAgainst" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SafeAgainst --version 8.0.0
#r "nuget: SafeAgainst, 8.0.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 SafeAgainst as a Cake Addin
#addin nuget:?package=SafeAgainst&version=8.0.0

// Install SafeAgainst as a Cake Tool
#tool nuget:?package=SafeAgainst&version=8.0.0

SafeAgainst

Provides functionality for creation of both synchronous and asynchronous generic guard clauses with custom preconditions and already contains implementation of basic ones for Collections, Enums, Strings, Objects and Numeric types.

Usage

  1. Eliminate Leading or trailing white spaces
  • passing value by reference:
    // Arrange
    var expectedValue = "Without leading or trailing whitespaces";
    var value = $"   {expectedValue}   ";

    // Act
    StringGuards.Safe.AgainstLeadingOrTrailingWhiteSpaces(ref value);

    // Assert
    Assert.Equal(expectedValue, value);
  • or using a SafeContainer:
    // Arrange
    var expectedValue = "Without leading or trailing whitespaces";
    var value = $"   {expectedValue}   ";
    var container = SafeContainer.Create(value);

    // Act
    StringGuards.Safe.AgainstLeadingOrTrailingWhiteSpaces(container);
    value = container.Value;

    // Assert
    Assert.Equal(expectedValue, value);
  1. Assigning a predefined value, if enum contains an invalid one
  • passing value by reference:
internal enum PowerOfTwo
{
    One = 1,
    Two = 2,
    Four = 4,
    Eight = 8,
    Sixteen = 16
};
    // Arrange
    PowerOfTwo powerOfTwo = default;
    var initialValue = powerOfTwo;

    // Act
    EnumGuards.Safe<PowerOfTwo>.AgainstNotInRange(ref powerOfTwo, PowerOfTwo.One);

    // Assert
    Assert.True(initialValue == 0);
    Assert.Equal(powerOfTwo, PowerOfTwo.One);
  • or using a SafeContainer:
    // Arrange
    PowerOfTwo powerOfTwo = default;
    var initialValue = powerOfTwo;
    var container = SafeContainer<PowerOfTwo>.Create(powerOfTwo);

    // Act
    EnumGuards.Safe<PowerOfTwo>.AgainstNotInRange(container, PowerOfTwo.One);
    powerOfTwo = container.Value;

    // Assert
    Assert.True(initialValue == 0);
    Assert.Equal(PowerOfTwo.One, powerOfTwo);
  1. Eliminating NULL elements from collection:
  • passing value by reference:
    // Arrange
    IEnumerable<int?> enumeration = Enumerable.Empty<int?>()
        .Append(2)
        .Append(10)
        .Append(null)
        .Append(1);

    // Act
    CollectionGuards.Safe<int?>.AgainstNullElements(ref enumeration);

    // Assert
    Assert.DoesNotContain(enumeration, x => x == null);
    Assert.Contains(enumeration, x => x != null);
    Assert.True(enumeration.ToList().Count == 3);
  • or using a SafeContainer:
    // Arrange
    IEnumerable<int?> enumeration = Enumerable.Empty<int?>()
        .Append(2)
        .Append(10)
        .Append(null)
        .Append(1);
    var container = SafeContainer.Create(enumeration);

    // Act
    CollectionGuards.Safe<int?>.AgainstNullElements(container);
    enumeration = container.Value;

    // Assert
    Assert.DoesNotContain(enumeration, x => x == null);
    Assert.Contains(enumeration, x => x != null);
    Assert.True(enumeration.ToList().Count == 3);

See way much more examples in the unit tests from the corresponding source repository.

Global Picture

General picture

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SafeAgainst:

Package Downloads
SafeAgainst.Extensions

Provides extension methods for both synchronous and asynchronous guard clauses for generic types, as well as for Enums, Objects, Collections, Strings, and various numeric types encapsulated in 'SafeContainer'.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 104 2/24/2024
7.0.0 99 2/17/2024
6.0.0 109 2/10/2024
5.0.0 92 2/4/2024
3.1.0 98 1/27/2024
3.0.0 114 1/17/2024
2.2.0 110 1/13/2024
2.1.0 124 1/7/2024
2.0.0 130 1/3/2024
1.1.0 129 12/30/2023
1.0.0 118 12/27/2023

Migrated to .Net Core 8.0