From 2a6bfadf58084b22c6d0ab2cea053c2d6914e747 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 4 May 2022 10:46:44 +0100 Subject: [PATCH] Call ArgumentNullException.ThrowIfNull --- X10D/src/Collections/ArrayExtensions.cs | 15 +-- X10D/src/Collections/BoolListExtensions.cs | 20 +--- X10D/src/Collections/DictionaryExtensions.cs | 111 ++++--------------- X10D/src/Collections/EnumerableExtensions.cs | 4 + X10D/src/Collections/ListExtensions.cs | 20 +--- X10D/src/Core/RandomExtensions.cs | 82 +++----------- X10D/src/Drawing/RandomExtensions.cs | 10 +- X10D/src/IO/FileInfoExtensions.cs | 12 +- X10D/src/IO/ListOfByteExtensions.cs | 56 ++-------- X10D/src/IO/StreamExtensions.cs | 57 ++++------ X10D/src/Linq/ByteExtensions.cs | 12 ++ X10D/src/Linq/DecimalExtensions.cs | 6 + X10D/src/Linq/DoubleExtensions.cs | 6 + X10D/src/Linq/Int32Extensions.cs | 12 ++ X10D/src/Linq/Int64Extensions.cs | 12 ++ X10D/src/Linq/ReadOnlySpanExtensions.cs | 10 +- X10D/src/Linq/SingleExtensions.cs | 6 + X10D/src/Linq/SpanExtensions.cs | 10 +- X10D/src/Math/ComparableExtensions.cs | 45 +++----- X10D/src/Net/EndPointExtensions.cs | 10 +- X10D/src/Net/IPAddressExtensions.cs | 6 + X10D/src/Numerics/RandomExtensions.cs | 20 +--- X10D/src/Reflection/MemberInfoExtensions.cs | 30 ++--- X10D/src/Reflection/TypeExtensions.cs | 44 +++++--- X10D/src/Text/StringExtensions.cs | 83 +++----------- 25 files changed, 225 insertions(+), 474 deletions(-) diff --git a/X10D/src/Collections/ArrayExtensions.cs b/X10D/src/Collections/ArrayExtensions.cs index 053d27d..7b6d5ee 100644 --- a/X10D/src/Collections/ArrayExtensions.cs +++ b/X10D/src/Collections/ArrayExtensions.cs @@ -17,10 +17,7 @@ public static class ArrayExtensions [Pure] public static IReadOnlyCollection AsReadOnly(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 /// is . public static void Clear(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 /// public static void Clear(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) { diff --git a/X10D/src/Collections/BoolListExtensions.cs b/X10D/src/Collections/BoolListExtensions.cs index 365778a..d2b8991 100644 --- a/X10D/src/Collections/BoolListExtensions.cs +++ b/X10D/src/Collections/BoolListExtensions.cs @@ -18,10 +18,7 @@ public static class BoolListExtensions [Pure] public static byte PackByte(this IReadOnlyList 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 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 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 source) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); if (source.Count > 64) { diff --git a/X10D/src/Collections/DictionaryExtensions.cs b/X10D/src/Collections/DictionaryExtensions.cs index fe0db27..2abef8a 100644 --- a/X10D/src/Collections/DictionaryExtensions.cs +++ b/X10D/src/Collections/DictionaryExtensions.cs @@ -34,15 +34,8 @@ public static class DictionaryExtensions Func 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 addValueFactory, Func 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 addValueFactory, Func 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(this IEnumerable> 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(this IEnumerable> source, Func 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 keySelector, Func 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(this IEnumerable> source) where TKey : notnull { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); static string GetQueryParameter(KeyValuePair pair) { @@ -361,15 +308,8 @@ public static class DictionaryExtensions Func 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 pair) @@ -407,20 +347,9 @@ public static class DictionaryExtensions Func keySelector, Func 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 pair) diff --git a/X10D/src/Collections/EnumerableExtensions.cs b/X10D/src/Collections/EnumerableExtensions.cs index baa8d19..1b46777 100644 --- a/X10D/src/Collections/EnumerableExtensions.cs +++ b/X10D/src/Collections/EnumerableExtensions.cs @@ -13,9 +13,13 @@ public static class EnumerableExtensions /// The element type. /// The to shuffle. /// Optional. The instance to use for the shuffling. + /// The shuffled collection. + /// is . [Pure] public static IReadOnlyCollection Shuffled(this IEnumerable source, Random? random = null) { + ArgumentNullException.ThrowIfNull(source); + var list = new List(source); list.Shuffle(random); return list.AsReadOnly(); diff --git a/X10D/src/Collections/ListExtensions.cs b/X10D/src/Collections/ListExtensions.cs index d943395..de3c0ab 100644 --- a/X10D/src/Collections/ListExtensions.cs +++ b/X10D/src/Collections/ListExtensions.cs @@ -17,10 +17,7 @@ public static class ListExtensions /// is . public static void Fill(this IList 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 /// public static void Fill(this IList 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(this IReadOnlyList 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 /// is . public static void Shuffle(this IList source, Random? random = null) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); random ??= System.Random.Shared; diff --git a/X10D/src/Core/RandomExtensions.cs b/X10D/src/Core/RandomExtensions.cs index a2c4ac9..c075fd3 100644 --- a/X10D/src/Core/RandomExtensions.cs +++ b/X10D/src/Core/RandomExtensions.cs @@ -21,10 +21,7 @@ public static class RandomExtensions public static T Next(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 /// is . 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 /// is less than 0. 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 /// 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 /// public static T NextFrom(this Random random, IEnumerable 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 /// is . 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 /// is . 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 /// 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 /// is . 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 /// is less than 0. 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 /// is . 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 /// is less than 0. 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 /// 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 /// is less than 0. public static string NextString(this Random random, IReadOnlyList 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) { diff --git a/X10D/src/Drawing/RandomExtensions.cs b/X10D/src/Drawing/RandomExtensions.cs index 32cf1e8..e121cb4 100644 --- a/X10D/src/Drawing/RandomExtensions.cs +++ b/X10D/src/Drawing/RandomExtensions.cs @@ -15,10 +15,7 @@ public static class RandomExtensions /// is . 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 /// is . 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); diff --git a/X10D/src/IO/FileInfoExtensions.cs b/X10D/src/IO/FileInfoExtensions.cs index 272c99a..2b36120 100644 --- a/X10D/src/IO/FileInfoExtensions.cs +++ b/X10D/src/IO/FileInfoExtensions.cs @@ -16,7 +16,7 @@ public static class FileInfoExtensions /// The type of the whose is to be used for /// computing the hash. /// - /// The hash of represented as an array of bytes. + /// The hash of represented as an array of bytes. /// is . /// The specified file was not found. /// The opened file stream cannot be read. @@ -29,10 +29,7 @@ public static class FileInfoExtensions public static byte[] GetHash(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(); @@ -65,10 +62,7 @@ public static class FileInfoExtensions public static bool TryWriteHash(this FileInfo value, Span 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(destination, out bytesWritten); diff --git a/X10D/src/IO/ListOfByteExtensions.cs b/X10D/src/IO/ListOfByteExtensions.cs index dbec6e1..1c8cf39 100644 --- a/X10D/src/IO/ListOfByteExtensions.cs +++ b/X10D/src/IO/ListOfByteExtensions.cs @@ -19,10 +19,7 @@ public static class ListOfByteExtensions /// is . public static string AsString(this IReadOnlyList 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 /// is . public static double ToDouble(this IReadOnlyList 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 /// is . public static short ToInt16(this IReadOnlyList 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 /// is . public static int ToInt32(this IReadOnlyList 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 /// is . public static long ToInt64(this IReadOnlyList 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 /// is . public static float ToSingle(this IReadOnlyList 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 /// public static string ToString(this IReadOnlyList 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 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 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 source, int startIndex) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return BitConverter.ToUInt64(source.ToArray(), startIndex); } diff --git a/X10D/src/IO/StreamExtensions.cs b/X10D/src/IO/StreamExtensions.cs index ab353de..2aa70bc 100644 --- a/X10D/src/IO/StreamExtensions.cs +++ b/X10D/src/IO/StreamExtensions.cs @@ -504,10 +504,7 @@ public static class StreamExtensions /// The number of bytes written to the stream. 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 /// The four-byte signed integer to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . 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 /// The stream to which the value should be written. /// The eight-byte signed integer to write. /// The number of bytes written to the stream. + /// is . public static int Write(this Stream stream, long value) { return stream.Write(value, DefaultEndianness); @@ -594,12 +590,10 @@ public static class StreamExtensions /// The eight-byte signed integer to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . 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 /// The stream to which the value should be written. /// The two-byte unsigned integer to write. /// The number of bytes written to the stream. + /// is . [CLSCompliant(false)] public static int Write(this Stream stream, ushort value) { @@ -641,13 +636,11 @@ public static class StreamExtensions /// The two-byte unsigned integer to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . [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 /// The stream to which the value should be written. /// The four-byte unsigned integer to write. /// The number of bytes written to the stream. + /// is . [CLSCompliant(false)] public static int Write(this Stream stream, uint value) { @@ -689,13 +683,11 @@ public static class StreamExtensions /// The four-byte unsigned integer to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . [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 /// The stream to which the value should be written. /// The eight-byte unsigned integer to write. /// The number of bytes written to the stream. + /// is . [CLSCompliant(false)] public static int Write(this Stream stream, ulong value) { @@ -737,13 +730,11 @@ public static class StreamExtensions /// The eight-byte signed integer to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . [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 /// The single-precision floating point value to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . 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 /// The double-precision floating point value to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . 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 /// The decimal value to write. /// The endian encoding to use. /// The number of bytes written to the stream. + /// is . 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)) { diff --git a/X10D/src/Linq/ByteExtensions.cs b/X10D/src/Linq/ByteExtensions.cs index ee049ff..32a11f3 100644 --- a/X10D/src/Linq/ByteExtensions.cs +++ b/X10D/src/Linq/ByteExtensions.cs @@ -12,8 +12,11 @@ public static class ByteExtensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static byte Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate((byte)1, (current, value) => (byte)(current * value)); } @@ -22,9 +25,12 @@ public static class ByteExtensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . [CLSCompliant(false)] public static sbyte Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate((sbyte)1, (current, value) => (sbyte)(current * value)); } @@ -36,8 +42,11 @@ public static class ByteExtensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static byte Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } @@ -49,9 +58,12 @@ public static class ByteExtensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . [CLSCompliant(false)] public static sbyte Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } diff --git a/X10D/src/Linq/DecimalExtensions.cs b/X10D/src/Linq/DecimalExtensions.cs index bcdf193..796a402 100644 --- a/X10D/src/Linq/DecimalExtensions.cs +++ b/X10D/src/Linq/DecimalExtensions.cs @@ -10,8 +10,11 @@ public static class DecimalExtensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static decimal Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1m, (current, value) => (current * value)); } @@ -23,8 +26,11 @@ public static class DecimalExtensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static decimal Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } } diff --git a/X10D/src/Linq/DoubleExtensions.cs b/X10D/src/Linq/DoubleExtensions.cs index 70ac1ca..e84343b 100644 --- a/X10D/src/Linq/DoubleExtensions.cs +++ b/X10D/src/Linq/DoubleExtensions.cs @@ -10,8 +10,11 @@ public static class DoubleExtensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static double Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1.0, (current, value) => (current * value)); } @@ -23,8 +26,11 @@ public static class DoubleExtensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static double Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } } diff --git a/X10D/src/Linq/Int32Extensions.cs b/X10D/src/Linq/Int32Extensions.cs index 923470e..fb636c8 100644 --- a/X10D/src/Linq/Int32Extensions.cs +++ b/X10D/src/Linq/Int32Extensions.cs @@ -12,8 +12,11 @@ public static class Int32Extensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static int Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1, (current, value) => current * value); } @@ -22,9 +25,12 @@ public static class Int32Extensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . [CLSCompliant(false)] public static uint Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1u, (current, value) => current * value); } @@ -36,8 +42,11 @@ public static class Int32Extensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static int Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } @@ -49,9 +58,12 @@ public static class Int32Extensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . [CLSCompliant(false)] public static uint Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } diff --git a/X10D/src/Linq/Int64Extensions.cs b/X10D/src/Linq/Int64Extensions.cs index 4870456..a29c3dc 100644 --- a/X10D/src/Linq/Int64Extensions.cs +++ b/X10D/src/Linq/Int64Extensions.cs @@ -12,8 +12,11 @@ public static class Int64Extensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static long Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1L, (current, value) => current * value); } @@ -22,9 +25,12 @@ public static class Int64Extensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . [CLSCompliant(false)] public static ulong Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1UL, (current, value) => current * value); } @@ -36,8 +42,11 @@ public static class Int64Extensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static long Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } @@ -49,9 +58,12 @@ public static class Int64Extensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . [CLSCompliant(false)] public static ulong Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } diff --git a/X10D/src/Linq/ReadOnlySpanExtensions.cs b/X10D/src/Linq/ReadOnlySpanExtensions.cs index 3bbbfca..9c465ea 100644 --- a/X10D/src/Linq/ReadOnlySpanExtensions.cs +++ b/X10D/src/Linq/ReadOnlySpanExtensions.cs @@ -21,10 +21,7 @@ public static class ReadOnlySpanExtensions [Pure] public static bool All(this ReadOnlySpan source, Predicate 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(this ReadOnlySpan source, Predicate predicate) { - if (predicate is null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); if (source.IsEmpty) { diff --git a/X10D/src/Linq/SingleExtensions.cs b/X10D/src/Linq/SingleExtensions.cs index 1797eaf..5cf9004 100644 --- a/X10D/src/Linq/SingleExtensions.cs +++ b/X10D/src/Linq/SingleExtensions.cs @@ -10,8 +10,11 @@ public static class SingleExtensions /// /// A sequence of values that are used to calculate the product. /// The product the values in the sequence. + /// is . public static float Product(this IEnumerable source) { + ArgumentNullException.ThrowIfNull(source); + return source.Aggregate(1f, (current, value) => (current * value)); } @@ -23,8 +26,11 @@ public static class SingleExtensions /// A transform function to apply to each element. /// The type of the elements of . /// The product of the projected values. + /// is . public static float Product(this IEnumerable source, Func selector) { + ArgumentNullException.ThrowIfNull(source); + return source.Select(selector).Product(); } } diff --git a/X10D/src/Linq/SpanExtensions.cs b/X10D/src/Linq/SpanExtensions.cs index 6de52f3..4ae07c0 100644 --- a/X10D/src/Linq/SpanExtensions.cs +++ b/X10D/src/Linq/SpanExtensions.cs @@ -21,10 +21,7 @@ public static class SpanExtensions [Pure] public static bool All(this Span source, Predicate 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(this Span source, Predicate predicate) { - if (predicate is null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); if (source.IsEmpty) { diff --git a/X10D/src/Math/ComparableExtensions.cs b/X10D/src/Math/ComparableExtensions.cs index 53bbc0e..211cfff 100644 --- a/X10D/src/Math/ComparableExtensions.cs +++ b/X10D/src/Math/ComparableExtensions.cs @@ -46,6 +46,7 @@ public static class ComparableExtensions /// // True /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool Between(this T1 value, T2 lower, T3 upper, @@ -54,10 +55,7 @@ public static class ComparableExtensions where T2 : IComparable where T3 : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static T Clamp(this T value, T lower, T upper) where T : IComparable { + ArgumentNullException.ThrowIfNull(value); + if (lower.GreaterThan(upper)) { throw new ArgumentException( @@ -138,15 +139,13 @@ public static class ComparableExtensions /// // result will be False /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool GreaterThan(this T1 value, T2 other) where T1 : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool GreaterThanOrEqualTo(this T1 value, T2 other) where T1 : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool LessThan(this T1 value, T2 other) where T1 : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool LessThanOrEqualTo(this T1 value, T2 other) where T1 : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static T Max(this T value, T other) where T : IComparable { - 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 /// /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static T Min(this T value, T other) where T : IComparable { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(value); return value.LessThan(other) ? value : other; } diff --git a/X10D/src/Net/EndPointExtensions.cs b/X10D/src/Net/EndPointExtensions.cs index b7ef2d7..81da9fc 100644 --- a/X10D/src/Net/EndPointExtensions.cs +++ b/X10D/src/Net/EndPointExtensions.cs @@ -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 { diff --git a/X10D/src/Net/IPAddressExtensions.cs b/X10D/src/Net/IPAddressExtensions.cs index 0b8358d..9b2320b 100644 --- a/X10D/src/Net/IPAddressExtensions.cs +++ b/X10D/src/Net/IPAddressExtensions.cs @@ -17,10 +17,13 @@ public static class IPAddressExtensions /// /// if the specified IP address is a valid IPv4 address; otherwise, . /// + /// is . [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 /// /// if the specified IP address is a valid IPv6 address; otherwise, . /// + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool IsIPv6(this IPAddress address) { + ArgumentNullException.ThrowIfNull(address); + return address.AddressFamily == AddressFamily.InterNetworkV6; } } diff --git a/X10D/src/Numerics/RandomExtensions.cs b/X10D/src/Numerics/RandomExtensions.cs index b89a5e2..ef94c17 100644 --- a/X10D/src/Numerics/RandomExtensions.cs +++ b/X10D/src/Numerics/RandomExtensions.cs @@ -19,10 +19,7 @@ public static class RandomExtensions /// is . 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 /// is . 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 /// 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 /// 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); diff --git a/X10D/src/Reflection/MemberInfoExtensions.cs b/X10D/src/Reflection/MemberInfoExtensions.cs index 9fd389e..826759c 100644 --- a/X10D/src/Reflection/MemberInfoExtensions.cs +++ b/X10D/src/Reflection/MemberInfoExtensions.cs @@ -25,10 +25,7 @@ public static class MemberInfoExtensions public static bool HasCustomAttribute(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()) { @@ -85,6 +75,9 @@ public static class MemberInfoExtensions Func selector) where TAttribute : Attribute { + ArgumentNullException.ThrowIfNull(member); + ArgumentNullException.ThrowIfNull(selector); + return member.SelectFromCustomAttribute(selector, default); } @@ -108,15 +101,8 @@ public static class MemberInfoExtensions Func 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() is { } attribute ? selector(attribute) diff --git a/X10D/src/Reflection/TypeExtensions.cs b/X10D/src/Reflection/TypeExtensions.cs index 301d1d2..001abc8 100644 --- a/X10D/src/Reflection/TypeExtensions.cs +++ b/X10D/src/Reflection/TypeExtensions.cs @@ -15,10 +15,13 @@ public static class TypeExtensions /// The type whose interface list to check. /// The interface type. /// if the current exists on the type; otherwise, . + /// is . [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool Implements(this Type value) { + ArgumentNullException.ThrowIfNull(value); + return value.Implements(typeof(T)); } @@ -28,19 +31,17 @@ public static class TypeExtensions /// The type whose interface list to check. /// The interface type. /// if the current exists on the type; otherwise, . + /// + /// is . + /// -or- + /// is . + /// [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 /// if the current type inherits , or /// otherwise. /// + /// is . + /// is not a class. [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool Inherits(this Type value) where T : class { + ArgumentNullException.ThrowIfNull(value); + return value.Inherits(typeof(T)); } @@ -79,19 +84,22 @@ public static class TypeExtensions /// if the current type inherits , or /// otherwise. /// + /// + /// is . + /// -or- + /// is . + /// + /// + /// is not a class. + /// -or- + /// is not a class. + /// [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) { diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index 2f83e3c..ba9a97e 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -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(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 Split(this string value, int chunkSize) { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(value); if (chunkSize == 0) {