Skip to content

Adds NullOrOutOfRange clauses, making a new option for Nullable classes/structs to OutOfRange. #142

@rafaelsc

Description

@rafaelsc

The current 3.2.0 Nuget Release. Don't have any support for Nullable classes/structs to OutOfRange

private static void TestStruct(int x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // OK
	Guard.Against.OutOfRange<int>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestNullableStruct(int? x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // Compiler Error
	Guard.Against.OutOfRange<int?>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestClass(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange(x, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(x, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

private static void TestClassWithNull(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

The current main branch code-base added some support to classes to OutOfRange, but not for Nullable.

private static void TestStruct(int x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // OK
	Guard.Against.OutOfRange<int>(x, nameof(x), -10, 10); // OK
}

private static void TestNullableStruct(int? x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // Compiler Error
	Guard.Against.OutOfRange<int?>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestClass(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);
	
	Guard.Against.OutOfRange(x, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	Guard.Against.OutOfRange<Version>(x, nameof(x), firstVersion, cuurentVersion);  // Compilation OK, but NullReferenceException in Runtime when x is null
}

private static void TestClassWithNull(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange((Version)null, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	Guard.Against.OutOfRange<Version>((Version)null, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	
	Guard.Against.OutOfRange(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

GuardClauses could add a NullOrOutOfRange clause to add support to Nullable classes/structs. And checking the input parameter fo null values, thowing ArgumentNullException insted of a NullReferenceException.

See this dotnetfiddle https://dotnetfiddle.net/nbXa8U
Kind Related: #139

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions