Merge pull request #53 from oliverbooth/52-use-argumentnullexceptionthrowifnull-instead-of-is-null-check

Use ArgumentNullException.ThrowIfNull instead of is null check (resolves #52)
This commit is contained in:
Oliver Booth 2022-05-04 10:54:23 +01:00 committed by GitHub
commit 2d51f65834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 225 additions and 474 deletions

View File

@ -17,10 +17,7 @@ public static class ArrayExtensions
[Pure]
public static IReadOnlyCollection<T> AsReadOnly<T>(this T[] array)
{
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
ArgumentNullException.ThrowIfNull(array);
return Array.AsReadOnly(array);
}
@ -45,10 +42,7 @@ public static class ArrayExtensions
/// <exception cref="ArgumentNullException"><paramref name="array" /> is <see langword="null" />.</exception>
public static void Clear<T>(this T?[] array, Range range)
{
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
ArgumentNullException.ThrowIfNull(array);
int index = range.Start.Value;
int end = range.End.Value;
@ -77,10 +71,7 @@ public static class ArrayExtensions
/// </exception>
public static void Clear<T>(this T?[] array, int index, int length)
{
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
ArgumentNullException.ThrowIfNull(array);
if (length == 0 || array.Length == 0)
{

View File

@ -18,10 +18,7 @@ public static class BoolListExtensions
[Pure]
public static byte PackByte(this IReadOnlyList<bool> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (source.Count > 8)
{
@ -48,10 +45,7 @@ public static class BoolListExtensions
[Pure]
public static short PackInt16(this IReadOnlyList<bool> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (source.Count > 16)
{
@ -78,10 +72,7 @@ public static class BoolListExtensions
[Pure]
public static int PackInt32(this IReadOnlyList<bool> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (source.Count > 32)
{
@ -108,10 +99,7 @@ public static class BoolListExtensions
[Pure]
public static long PackInt64(this IReadOnlyList<bool> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (source.Count > 64)
{

View File

@ -34,15 +34,8 @@ public static class DictionaryExtensions
Func<TKey, TValue, TValue> updateValueFactory)
where TKey : notnull
{
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
ArgumentNullException.ThrowIfNull(dictionary);
ArgumentNullException.ThrowIfNull(updateValueFactory);
if (dictionary.ContainsKey(key))
{
@ -84,20 +77,9 @@ public static class DictionaryExtensions
Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
where TKey : notnull
{
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
if (addValueFactory is null)
{
throw new ArgumentNullException(nameof(addValueFactory));
}
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
ArgumentNullException.ThrowIfNull(dictionary);
ArgumentNullException.ThrowIfNull(addValueFactory);
ArgumentNullException.ThrowIfNull(updateValueFactory);
if (dictionary.ContainsKey(key))
{
@ -145,20 +127,9 @@ public static class DictionaryExtensions
Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)
where TKey : notnull
{
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
if (addValueFactory is null)
{
throw new ArgumentNullException(nameof(addValueFactory));
}
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
ArgumentNullException.ThrowIfNull(dictionary);
ArgumentNullException.ThrowIfNull(addValueFactory);
ArgumentNullException.ThrowIfNull(updateValueFactory);
if (dictionary.ContainsKey(key))
{
@ -184,10 +155,7 @@ public static class DictionaryExtensions
[Pure]
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
static string SanitizeValue(string? value)
{
@ -227,15 +195,8 @@ public static class DictionaryExtensions
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TValue, string?> selector)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(selector);
static string SanitizeValue(string? value)
{
@ -281,20 +242,9 @@ public static class DictionaryExtensions
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (keySelector is null)
{
throw new ArgumentNullException(nameof(keySelector));
}
if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(keySelector);
ArgumentNullException.ThrowIfNull(valueSelector);
static string SanitizeValue(string? value)
{
@ -326,10 +276,7 @@ public static class DictionaryExtensions
public static string ToGetParameters<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
static string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
@ -361,15 +308,8 @@ public static class DictionaryExtensions
Func<TValue, string?> selector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(selector);
// can't static here because of 'selector' parameter
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
@ -407,20 +347,9 @@ public static class DictionaryExtensions
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (keySelector is null)
{
throw new ArgumentNullException(nameof(keySelector));
}
if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(keySelector);
ArgumentNullException.ThrowIfNull(valueSelector);
// can't static here because of selector parameters
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)

View File

@ -134,9 +134,13 @@ public static class EnumerableExtensions
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}" /> to shuffle.</param>
/// <param name="random">Optional. The <see cref="System.Random" /> instance to use for the shuffling.</param>
/// <returns>The shuffled collection.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[Pure]
public static IReadOnlyCollection<T> Shuffled<T>(this IEnumerable<T> source, Random? random = null)
{
ArgumentNullException.ThrowIfNull(source);
var list = new List<T>(source);
list.Shuffle(random);
return list.AsReadOnly();

View File

@ -17,10 +17,7 @@ public static class ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static void Fill<T>(this IList<T> source, T value)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
for (var i = 0; i < source.Count; i++)
{
@ -47,10 +44,7 @@ public static class ListExtensions
/// </exception>
public static void Fill<T>(this IList<T> source, T value, int startIndex, int count)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (startIndex < 0)
{
@ -98,10 +92,7 @@ public static class ListExtensions
[Pure]
public static T Random<T>(this IReadOnlyList<T> source, Random? random = null)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
random ??= System.Random.Shared;
return random.NextFrom(source);
@ -119,10 +110,7 @@ public static class ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static void Shuffle<T>(this IList<T> source, Random? random = null)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
random ??= System.Random.Shared;

View File

@ -21,10 +21,7 @@ public static class RandomExtensions
public static T Next<T>(this Random random)
where T : struct, Enum
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
var values = Enum.GetValues(typeof(T));
return (T)values.GetValue(random.Next(values.Length))!;
@ -44,10 +41,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static bool NextBoolean(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return random.NextDouble() >= 0.5;
}
@ -67,10 +61,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static double NextDouble(this Random random, double maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
if (maxValue < 0)
{
@ -99,10 +90,7 @@ public static class RandomExtensions
/// </exception>
public static double NextDouble(this Random random, double minValue, double maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
if (maxValue < minValue)
{
@ -133,15 +121,8 @@ public static class RandomExtensions
/// </example>
public static T NextFrom<T>(this Random random, IEnumerable<T> source)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(random);
ArgumentNullException.ThrowIfNull(source);
if (source is T[] array)
{
@ -166,10 +147,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return random.NextByte(byte.MaxValue);
}
@ -190,10 +168,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random, byte maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return random.NextByte(0, maxValue);
}
@ -219,10 +194,7 @@ public static class RandomExtensions
/// </exception>
public static byte NextByte(this Random random, byte minValue, byte maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return (byte)random.Next(minValue, maxValue);
}
@ -237,10 +209,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return random.NextInt16(short.MaxValue);
}
@ -262,10 +231,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static short NextInt16(this Random random, short maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
if (maxValue < 0)
{
@ -296,10 +262,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random, short minValue, short maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
return (short)random.Next(minValue, maxValue);
}
@ -319,10 +282,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentException"><paramref name="maxValue" /> is less than 0.</exception>
public static float NextSingle(this Random random, float maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
if (maxValue < 0)
{
@ -351,10 +311,7 @@ public static class RandomExtensions
/// </exception>
public static float NextSingle(this Random random, float minValue, float maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
if (maxValue < minValue)
{
@ -382,15 +339,8 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is less than 0.</exception>
public static string NextString(this Random random, IReadOnlyList<char> source, int length)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(random);
ArgumentNullException.ThrowIfNull(source);
if (length < 0)
{

View File

@ -15,10 +15,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorRgb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
int rgb = random.Next();
return Color.FromArgb(0xFF, (byte)(rgb >> 16 & 0xFF), (byte)(rgb >> 8 & 0xFF), (byte)(rgb & 0xFF));
@ -32,10 +29,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorArgb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
int argb = random.Next();
return Color.FromArgb(argb);

View File

@ -16,7 +16,7 @@ public static class FileInfoExtensions
/// The type of the <see cref="HashAlgorithm" /> whose <see cref="HashAlgorithm.ComputeHash(Stream)" /> is to be used for
/// computing the hash.
/// </typeparam>
/// <returns>The hash of <paramref name="stream" /> represented as an array of bytes.</returns>
/// <returns>The hash of <paramref name="value" /> represented as an array of bytes.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
/// <exception cref="FileNotFoundException">The specified file was not found.</exception>
/// <exception cref="IOException">The opened file stream cannot be read.</exception>
@ -29,10 +29,7 @@ public static class FileInfoExtensions
public static byte[] GetHash<T>(this FileInfo value)
where T : HashAlgorithm
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
using FileStream stream = value.OpenRead();
return stream.GetHash<T>();
@ -65,10 +62,7 @@ public static class FileInfoExtensions
public static bool TryWriteHash<T>(this FileInfo value, Span<byte> destination, out int bytesWritten)
where T : HashAlgorithm
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
using FileStream stream = value.OpenRead();
return stream.TryWriteHash<T>(destination, out bytesWritten);

View File

@ -19,10 +19,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static string AsString(this IReadOnlyList<byte> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToString(source.ToArray());
}
@ -50,10 +47,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double ToDouble(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToDouble(source.ToArray(), startIndex);
}
@ -78,10 +72,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static short ToInt16(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToInt16(source.ToArray(), startIndex);
}
@ -106,10 +97,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int ToInt32(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToInt32(source.ToArray(), startIndex);
}
@ -134,10 +122,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long ToInt64(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToInt64(source.ToArray(), startIndex);
}
@ -164,10 +149,7 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float ToSingle(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToSingle(source.ToArray(), startIndex);
}
@ -185,15 +167,8 @@ public static class ListOfByteExtensions
/// </exception>
public static string ToString(this IReadOnlyList<byte> source, Encoding encoding)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(encoding);
return encoding.GetString(source.ToArray());
}
@ -220,10 +195,7 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static ushort ToUInt16(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToUInt16(source.ToArray(), startIndex);
}
@ -250,10 +222,7 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static uint ToUInt32(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToUInt32(source.ToArray(), startIndex);
}
@ -280,10 +249,7 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static ulong ToUInt64(this IReadOnlyList<byte> source, int startIndex)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
return BitConverter.ToUInt64(source.ToArray(), startIndex);
}

View File

@ -504,10 +504,7 @@ public static class StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, short value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -548,12 +545,10 @@ public static class StreamExtensions
/// <param name="value">The four-byte signed integer to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, int value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -581,6 +576,7 @@ public static class StreamExtensions
/// <param name="stream">The stream to which the value should be written.</param>
/// <param name="value">The eight-byte signed integer to write.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, long value)
{
return stream.Write(value, DefaultEndianness);
@ -594,12 +590,10 @@ public static class StreamExtensions
/// <param name="value">The eight-byte signed integer to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, long value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -627,6 +621,7 @@ public static class StreamExtensions
/// <param name="stream">The stream to which the value should be written.</param>
/// <param name="value">The two-byte unsigned integer to write.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, ushort value)
{
@ -641,13 +636,11 @@ public static class StreamExtensions
/// <param name="value">The two-byte unsigned integer to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, ushort value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -675,6 +668,7 @@ public static class StreamExtensions
/// <param name="stream">The stream to which the value should be written.</param>
/// <param name="value">The four-byte unsigned integer to write.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, uint value)
{
@ -689,13 +683,11 @@ public static class StreamExtensions
/// <param name="value">The four-byte unsigned integer to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, uint value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -723,6 +715,7 @@ public static class StreamExtensions
/// <param name="stream">The stream to which the value should be written.</param>
/// <param name="value">The eight-byte unsigned integer to write.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, ulong value)
{
@ -737,13 +730,11 @@ public static class StreamExtensions
/// <param name="value">The eight-byte signed integer to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static int Write(this Stream stream, ulong value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -772,12 +763,10 @@ public static class StreamExtensions
/// <param name="value">The single-precision floating point value to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, float value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -806,12 +795,10 @@ public static class StreamExtensions
/// <param name="value">The double-precision floating point value to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, double value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{
@ -840,12 +827,10 @@ public static class StreamExtensions
/// <param name="value">The decimal value to write.</param>
/// <param name="endianness">The endian encoding to use.</param>
/// <returns>The number of bytes written to the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, decimal value, Endianness endianness)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
ArgumentNullException.ThrowIfNull(stream);
if (!Enum.IsDefined(endianness))
{

View File

@ -12,8 +12,11 @@ public static class ByteExtensions
/// </summary>
/// <param name="source">A sequence of <see cref="byte" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static byte Product(this IEnumerable<byte> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate((byte)1, (current, value) => (byte)(current * value));
}
@ -22,9 +25,12 @@ public static class ByteExtensions
/// </summary>
/// <param name="source">A sequence of <see cref="sbyte" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static sbyte Product(this IEnumerable<sbyte> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate((sbyte)1, (current, value) => (sbyte)(current * value));
}
@ -36,8 +42,11 @@ public static class ByteExtensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static byte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, byte> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
@ -49,9 +58,12 @@ public static class ByteExtensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static sbyte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, sbyte> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}

View File

@ -10,8 +10,11 @@ public static class DecimalExtensions
/// </summary>
/// <param name="source">A sequence of <see cref="decimal" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static decimal Product(this IEnumerable<decimal> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1m, (current, value) => (current * value));
}
@ -23,8 +26,11 @@ public static class DecimalExtensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static decimal Product<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
}

View File

@ -10,8 +10,11 @@ public static class DoubleExtensions
/// </summary>
/// <param name="source">A sequence of <see cref="double" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double Product(this IEnumerable<double> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1.0, (current, value) => (current * value));
}
@ -23,8 +26,11 @@ public static class DoubleExtensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double Product<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
}

View File

@ -12,8 +12,11 @@ public static class Int32Extensions
/// </summary>
/// <param name="source">A sequence of <see cref="int" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int Product(this IEnumerable<int> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1, (current, value) => current * value);
}
@ -22,9 +25,12 @@ public static class Int32Extensions
/// </summary>
/// <param name="source">A sequence of <see cref="uint" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static uint Product(this IEnumerable<uint> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1u, (current, value) => current * value);
}
@ -36,8 +42,11 @@ public static class Int32Extensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int Product<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
@ -49,9 +58,12 @@ public static class Int32Extensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static uint Product<TSource>(this IEnumerable<TSource> source, Func<TSource, uint> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}

View File

@ -12,8 +12,11 @@ public static class Int64Extensions
/// </summary>
/// <param name="source">A sequence of <see cref="long" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long Product(this IEnumerable<long> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1L, (current, value) => current * value);
}
@ -22,9 +25,12 @@ public static class Int64Extensions
/// </summary>
/// <param name="source">A sequence of <see cref="ulong" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ulong Product(this IEnumerable<ulong> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1UL, (current, value) => current * value);
}
@ -36,8 +42,11 @@ public static class Int64Extensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long Product<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
@ -49,9 +58,12 @@ public static class Int64Extensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[CLSCompliant(false)]
public static ulong Product<TSource>(this IEnumerable<TSource> source, Func<TSource, ulong> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}

View File

@ -21,10 +21,7 @@ public static class ReadOnlySpanExtensions
[Pure]
public static bool All<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
ArgumentNullException.ThrowIfNull(predicate);
if (source.IsEmpty)
{
@ -56,10 +53,7 @@ public static class ReadOnlySpanExtensions
[Pure]
public static bool Any<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
ArgumentNullException.ThrowIfNull(predicate);
if (source.IsEmpty)
{

View File

@ -10,8 +10,11 @@ public static class SingleExtensions
/// </summary>
/// <param name="source">A sequence of <see cref="float" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float Product(this IEnumerable<float> source)
{
ArgumentNullException.ThrowIfNull(source);
return source.Aggregate(1f, (current, value) => (current * value));
}
@ -23,8 +26,11 @@ public static class SingleExtensions
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float Product<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select(selector).Product();
}
}

View File

@ -21,10 +21,7 @@ public static class SpanExtensions
[Pure]
public static bool All<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
ArgumentNullException.ThrowIfNull(predicate);
if (source.IsEmpty)
{
@ -56,10 +53,7 @@ public static class SpanExtensions
[Pure]
public static bool Any<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
ArgumentNullException.ThrowIfNull(predicate);
if (source.IsEmpty)
{

View File

@ -46,6 +46,7 @@ public static class ComparableExtensions
/// // True
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Between<T1, T2, T3>(this T1 value, T2 lower, T3 upper,
@ -54,10 +55,7 @@ public static class ComparableExtensions
where T2 : IComparable<T3>
where T3 : IComparable<T2>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
if (lower.GreaterThan(upper))
{
@ -102,11 +100,14 @@ public static class ComparableExtensions
/// // clamped will be 20
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static T Clamp<T>(this T value, T lower, T upper)
where T : IComparable<T>
{
ArgumentNullException.ThrowIfNull(value);
if (lower.GreaterThan(upper))
{
throw new ArgumentException(
@ -138,15 +139,13 @@ public static class ComparableExtensions
/// // result will be False
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool GreaterThan<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.CompareTo(other) > 0;
}
@ -172,15 +171,13 @@ public static class ComparableExtensions
/// // result will be False
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool GreaterThanOrEqualTo<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.CompareTo(other) >= 0;
}
@ -206,15 +203,13 @@ public static class ComparableExtensions
/// // result will be True
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool LessThan<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.CompareTo(other) < 0;
}
@ -240,15 +235,13 @@ public static class ComparableExtensions
/// // result will be True
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool LessThanOrEqualTo<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.CompareTo(other) <= 0;
}
@ -273,15 +266,13 @@ public static class ComparableExtensions
/// // max will be 10
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static T Max<T>(this T value, T other)
where T : IComparable<T>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.GreaterThan(other) ? value : other;
}
@ -306,15 +297,13 @@ public static class ComparableExtensions
/// // min will be 5
/// </code>
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static T Min<T>(this T value, T other)
where T : IComparable<T>
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return value.LessThan(other) ? value : other;
}

View File

@ -25,10 +25,7 @@ public static class EndPointExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string GetHost(this EndPoint endPoint)
{
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
ArgumentNullException.ThrowIfNull(endPoint);
return endPoint switch
{
@ -54,10 +51,7 @@ public static class EndPointExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int GetPort(this EndPoint endPoint)
{
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
ArgumentNullException.ThrowIfNull(endPoint);
return endPoint switch
{

View File

@ -17,10 +17,13 @@ public static class IPAddressExtensions
/// <returns>
/// <see langword="true" /> if the specified IP address is a valid IPv4 address; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="address" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsIPv4(this IPAddress address)
{
ArgumentNullException.ThrowIfNull(address);
return address.AddressFamily == AddressFamily.InterNetwork;
}
@ -31,10 +34,13 @@ public static class IPAddressExtensions
/// <returns>
/// <see langword="true" /> if the specified IP address is a valid IPv6 address; otherwise, <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="address" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsIPv6(this IPAddress address)
{
ArgumentNullException.ThrowIfNull(address);
return address.AddressFamily == AddressFamily.InterNetworkV6;
}
}

View File

@ -19,10 +19,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotation(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
int seed = random.Next();
var seededRandom = new Random(seed);
@ -42,10 +39,7 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotationUniform(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
int seed = random.Next();
var seededRandom = new Random(seed);
@ -74,10 +68,7 @@ public static class RandomExtensions
/// </returns>
public static Vector2 NextUnitVector2(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
// no need to construct a seeded random here, since we only call Next once
@ -98,10 +89,7 @@ public static class RandomExtensions
/// </returns>
public static Vector3 NextUnitVector3(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
ArgumentNullException.ThrowIfNull(random);
int seed = random.Next();
var seededRandom = new Random(seed);

View File

@ -25,10 +25,7 @@ public static class MemberInfoExtensions
public static bool HasCustomAttribute<T>(this MemberInfo member)
where T : Attribute
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
ArgumentNullException.ThrowIfNull(member);
return member.HasCustomAttribute(typeof(T));
}
@ -47,15 +44,8 @@ public static class MemberInfoExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool HasCustomAttribute(this MemberInfo member, Type attribute)
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
if (attribute is null)
{
throw new ArgumentNullException(nameof(attribute));
}
ArgumentNullException.ThrowIfNull(member);
ArgumentNullException.ThrowIfNull(attribute);
if (!attribute.Inherits<Attribute>())
{
@ -85,6 +75,9 @@ public static class MemberInfoExtensions
Func<TAttribute, TReturn> selector)
where TAttribute : Attribute
{
ArgumentNullException.ThrowIfNull(member);
ArgumentNullException.ThrowIfNull(selector);
return member.SelectFromCustomAttribute(selector, default);
}
@ -108,15 +101,8 @@ public static class MemberInfoExtensions
Func<TAttribute, TReturn> selector, TReturn? defaultValue)
where TAttribute : Attribute
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
ArgumentNullException.ThrowIfNull(member);
ArgumentNullException.ThrowIfNull(selector);
return member.GetCustomAttribute<TAttribute>() is { } attribute
? selector(attribute)

View File

@ -15,10 +15,13 @@ public static class TypeExtensions
/// <param name="value">The type whose interface list to check.</param>
/// <typeparam name="T">The interface type.</typeparam>
/// <returns><see langword="true" /> if the current exists on the type; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Implements<T>(this Type value)
{
ArgumentNullException.ThrowIfNull(value);
return value.Implements(typeof(T));
}
@ -28,19 +31,17 @@ public static class TypeExtensions
/// <param name="value">The type whose interface list to check.</param>
/// <param name="interfaceType">The interface type.</param>
/// <returns><see langword="true" /> if the current exists on the type; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="value" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="interfaceType" /> is <see langword="null" />.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Implements(this Type value, Type interfaceType)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (interfaceType is null)
{
throw new ArgumentNullException(nameof(interfaceType));
}
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(interfaceType);
if (!interfaceType.IsInterface)
{
@ -62,11 +63,15 @@ public static class TypeExtensions
/// <see langword="true" /> if the current type inherits <typeparamref name="T" />, or <see langword="false" />
/// otherwise.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="value" /> is not a class.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Inherits<T>(this Type value)
where T : class
{
ArgumentNullException.ThrowIfNull(value);
return value.Inherits(typeof(T));
}
@ -79,19 +84,22 @@ public static class TypeExtensions
/// <see langword="true" /> if the current type inherits <paramref name="type" />, or <see langword="false" />
/// otherwise.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="value" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="type" /> is <see langword="null" />.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// <para><paramref name="value" /> is not a class.</para>
/// -or-
/// <para><paramref name="type" /> is not a class.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Inherits(this Type value, Type type)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(type);
if (!value.IsClass)
{

View File

@ -57,10 +57,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Base64Decode(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return Convert.FromBase64String(value).ToString(Encoding.ASCII);
}
@ -75,10 +72,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Base64Encode(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
return Convert.ToBase64String(value.GetBytes(Encoding.ASCII));
}
@ -104,20 +98,9 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string ChangeEncoding(this string value, Encoding sourceEncoding, Encoding destinationEncoding)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (sourceEncoding is null)
{
throw new ArgumentNullException(nameof(sourceEncoding));
}
if (destinationEncoding is null)
{
throw new ArgumentNullException(nameof(destinationEncoding));
}
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(sourceEncoding);
ArgumentNullException.ThrowIfNull(destinationEncoding);
return value.GetBytes(sourceEncoding).ToString(destinationEncoding);
}
@ -156,10 +139,7 @@ public static class StringExtensions
public static T EnumParse<T>(this string value, bool ignoreCase)
where T : struct, Enum
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
value = value.Trim();
@ -214,15 +194,8 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static byte[] GetBytes(this string value, Encoding encoding)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(encoding);
return encoding.GetBytes(value);
}
@ -238,10 +211,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsLower(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
for (var index = 0; index < value.Length; index++)
{
@ -275,10 +245,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsPalindrome(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
if (string.IsNullOrWhiteSpace(value))
{
@ -323,10 +290,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static bool IsUpper(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
for (var index = 0; index < value.Length; index++)
{
@ -358,10 +322,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Repeat(this string value, int count)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
switch (count)
{
@ -399,10 +360,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Randomize(this string source, int length, Random? random = null)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);
if (length < 0)
{
@ -437,10 +395,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Reverse(this string value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
if (value.Length < 2)
{
@ -471,10 +426,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static string Shuffled(this string value, Random? random = null)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
random ??= Random.Shared;
@ -497,10 +449,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static IEnumerable<string> Split(this string value, int chunkSize)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);
if (chunkSize == 0)
{