From 209309541aa5a9b1830ce9fd3d60cfa082e65c87 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 14 Nov 2024 10:58:23 +1100 Subject: [PATCH] ArgumentException.ThrowIfNullOrEmpty --- api_list.include.md | 2 +- readme.md | 4 ++-- src/Polyfill/Guard_NotEmpty.cs | 7 ++++--- src/Polyfill/Guard_NotNullOrEmpty.cs | 5 +++++ src/Polyfill/Guard_NotWhiteSpace.cs | 1 - 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/api_list.include.md b/api_list.include.md index 380d3ab8..096d8532 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -24,7 +24,7 @@ #### ConcurrentBag - * `void Clear(ConcurrentBag)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentbag-1.clear) + * `void Clear()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentbag-1.clear) #### ConcurrentDictionary diff --git a/readme.md b/readme.md index 88bffa42..99a50283 100644 --- a/readme.md +++ b/readme.md @@ -330,9 +330,9 @@ using System.Diagnostics.CodeAnalysis; struct UnscopedRefUsage { - int field; + int field1; - [UnscopedRef] ref int Prop1 => ref field; + [UnscopedRef] ref int Prop1 => ref field1; } ``` snippet source | anchor diff --git a/src/Polyfill/Guard_NotEmpty.cs b/src/Polyfill/Guard_NotEmpty.cs index 69a511f1..60267241 100644 --- a/src/Polyfill/Guard_NotEmpty.cs +++ b/src/Polyfill/Guard_NotEmpty.cs @@ -19,14 +19,15 @@ static partial class Guard { public static void NotEmpty(string? value, [CallerArgumentExpression("value")] string argumentName = "") { -#if NET7_0_OR_GREATER - ArgumentException.ThrowIfNullOrEmpty(value, argumentName); -#else if (value is null) { return; } +#if NET7_0_OR_GREATER + ArgumentException.ThrowIfNullOrEmpty(value, argumentName); +#else + if (value.Length == 0) { throw new ArgumentException("Argument cannot be empty.", argumentName); diff --git a/src/Polyfill/Guard_NotNullOrEmpty.cs b/src/Polyfill/Guard_NotNullOrEmpty.cs index 16c972b0..3b1ae884 100644 --- a/src/Polyfill/Guard_NotNullOrEmpty.cs +++ b/src/Polyfill/Guard_NotNullOrEmpty.cs @@ -19,6 +19,10 @@ public static string NotNullOrEmpty( [NotNull] string? value, [CallerArgumentExpression("value")] string argumentName = "") { +#if NET7_0_OR_GREATER + ArgumentException.ThrowIfNullOrEmpty(value, argumentName); + return value; +#else if (value is null) { throw new ArgumentNullException(argumentName); @@ -30,6 +34,7 @@ public static string NotNullOrEmpty( } return value; +#endif } public static T NotNullOrEmpty( diff --git a/src/Polyfill/Guard_NotWhiteSpace.cs b/src/Polyfill/Guard_NotWhiteSpace.cs index 063d41c4..4026a987 100644 --- a/src/Polyfill/Guard_NotWhiteSpace.cs +++ b/src/Polyfill/Guard_NotWhiteSpace.cs @@ -24,7 +24,6 @@ public static void NotWhiteSpace( } #if NET8_0_OR_GREATER ArgumentException.ThrowIfNullOrWhiteSpace(value, argumentName); - return value; #else if (value.Length == 0)