1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 02:45:41 +00:00

fix: use ArgumentNullException throw helper for .NET >=6

Some of these directives were incorrectly written as #if NET6_0, when ThrowIfNull is available in all future versions too. The macro has been fixed to #if NET6_0_OR_GREATER.

For other methods, the branch has been introduced where it didn't exist before.
This commit is contained in:
Oliver Booth 2023-04-01 14:19:28 +01:00
parent dd325ba5c9
commit 431e72a4c1
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
4 changed files with 39 additions and 5 deletions

View File

@ -16,10 +16,14 @@ public static class CollectionExtensions
/// <seealso cref="EnumerableExtensions.DisposeAll{T}" /> /// <seealso cref="EnumerableExtensions.DisposeAll{T}" />
public static void ClearAndDisposeAll<T>(this ICollection<T> source) where T : IDisposable public static void ClearAndDisposeAll<T>(this ICollection<T> source) where T : IDisposable
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
#endif
if (source.IsReadOnly) if (source.IsReadOnly)
{ {
@ -51,10 +55,14 @@ public static class CollectionExtensions
/// <seealso cref="EnumerableExtensions.DisposeAllAsync{T}" /> /// <seealso cref="EnumerableExtensions.DisposeAllAsync{T}" />
public static async Task ClearAndDisposeAllAsync<T>(this ICollection<T> source) where T : IAsyncDisposable public static async Task ClearAndDisposeAllAsync<T>(this ICollection<T> source) where T : IAsyncDisposable
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
#endif
if (source.IsReadOnly) if (source.IsReadOnly)
{ {

View File

@ -24,7 +24,7 @@ public static class EnumerableExtensions
[Pure] [Pure]
public static int CountWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static int CountWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{ {
#if NET6_0 #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate); ArgumentNullException.ThrowIfNull(predicate);
#else #else
@ -58,7 +58,7 @@ public static class EnumerableExtensions
[Pure] [Pure]
public static TSource FirstWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static TSource FirstWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{ {
#if NET6_0 #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate); ArgumentNullException.ThrowIfNull(predicate);
#else #else
@ -91,7 +91,7 @@ public static class EnumerableExtensions
[Pure] [Pure]
public static TSource? FirstWhereNotOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static TSource? FirstWhereNotOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{ {
#if NET6_0 #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate); ArgumentNullException.ThrowIfNull(predicate);
#else #else
@ -127,6 +127,10 @@ public static class EnumerableExtensions
/// </exception> /// </exception>
public static void For<T>(this IEnumerable<T> source, Action<int, T> action) public static void For<T>(this IEnumerable<T> source, Action<int, T> action)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(action);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
@ -136,6 +140,7 @@ public static class EnumerableExtensions
{ {
throw new ArgumentNullException(nameof(action)); throw new ArgumentNullException(nameof(action));
} }
#endif
var index = 0; var index = 0;
foreach (T item in source) foreach (T item in source)
@ -161,6 +166,10 @@ public static class EnumerableExtensions
/// </exception> /// </exception>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(action);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
@ -170,6 +179,7 @@ public static class EnumerableExtensions
{ {
throw new ArgumentNullException(nameof(action)); throw new ArgumentNullException(nameof(action));
} }
#endif
foreach (T item in source) foreach (T item in source)
{ {
@ -186,10 +196,14 @@ public static class EnumerableExtensions
/// <seealso cref="CollectionExtensions.ClearAndDisposeAll{T}" /> /// <seealso cref="CollectionExtensions.ClearAndDisposeAll{T}" />
public static void DisposeAll<T>(this IEnumerable<T> source) where T : IDisposable public static void DisposeAll<T>(this IEnumerable<T> source) where T : IDisposable
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
#endif
foreach (T item in source) foreach (T item in source)
{ {
@ -213,10 +227,14 @@ public static class EnumerableExtensions
/// <seealso cref="CollectionExtensions.ClearAndDisposeAllAsync{T}" /> /// <seealso cref="CollectionExtensions.ClearAndDisposeAllAsync{T}" />
public static async Task DisposeAllAsync<T>(this IEnumerable<T> source) where T : IAsyncDisposable public static async Task DisposeAllAsync<T>(this IEnumerable<T> source) where T : IAsyncDisposable
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null) if (source is null)
{ {
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
#endif
foreach (T item in source) foreach (T item in source)
{ {
@ -246,7 +264,7 @@ public static class EnumerableExtensions
[Pure] [Pure]
public static TSource LastWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static TSource LastWhereNot<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{ {
#if NET6_0 #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate); ArgumentNullException.ThrowIfNull(predicate);
#else #else
@ -279,7 +297,7 @@ public static class EnumerableExtensions
[Pure] [Pure]
public static TSource? LastWhereNotOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static TSource? LastWhereNotOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{ {
#if NET6_0 #if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate); ArgumentNullException.ThrowIfNull(predicate);
#else #else

View File

@ -55,10 +55,14 @@ public class StringBuilderReader : TextReader
/// <inheritdoc /> /// <inheritdoc />
public override int Read(char[] buffer, int index, int count) public override int Read(char[] buffer, int index, int count)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(buffer);
#else
if (buffer is null) if (buffer is null)
{ {
throw new ArgumentNullException(nameof(buffer)); throw new ArgumentNullException(nameof(buffer));
} }
#endif
if (index < 0) if (index < 0)
{ {

View File

@ -64,10 +64,14 @@ public static class StringExtensions
#endif #endif
public static TimeSpan ToTimeSpan(this string input) public static TimeSpan ToTimeSpan(this string input)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(input);
#else
if (input is null) if (input is null)
{ {
throw new ArgumentNullException(nameof(input)); throw new ArgumentNullException(nameof(input));
} }
#endif
return TimeSpanParser.TryParse(input, out TimeSpan result) return TimeSpanParser.TryParse(input, out TimeSpan result)
? result ? result