Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rafaelsc opened this issue Oct 25, 2021 · 1 comment

Comments

@rafaelsc
Copy link
Contributor

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

@ardalis
Copy link
Owner

ardalis commented Jan 18, 2022

I should be able to quickly add this to a 4.1 release if you want to update your PR. Thanks!

@ardalis ardalis closed this as completed Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants