diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs index 9a970480fba7f..1bd94635a26c7 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs @@ -291,10 +291,7 @@ public static int GetGeneration(WeakReference wo) object? obj = GCHandle.InternalGet(wo.WeakHandle); KeepAlive(wo); - if (obj is null) - { - throw new ArgumentNullException(nameof(wo)); - } + ArgumentNullException.ThrowIfNull(obj, nameof(wo)); return GetGeneration(obj); } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs index 2ba8e7a1388f0..ae863a7c034dd 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs @@ -276,8 +276,8 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie { for (int i = 0; i < optionalCustomModifiers.Length; i++) { - Type t = optionalCustomModifiers[i] ?? - throw new ArgumentNullException(nameof(optionalCustomModifiers)); + Type t = optionalCustomModifiers[i]; + ArgumentNullException.ThrowIfNull(t, nameof(optionalCustomModifiers)); if (t.HasElementType) throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(optionalCustomModifiers)); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index 6a4f639d8eb08..a610a5cbe3eab 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -1879,7 +1879,7 @@ private static object CreateCustomAttributeInstance(RuntimeModule module, Runtim { if (module is null) { - throw new ArgumentNullException(SR.Arg_InvalidHandle); + throw new ArgumentNullException(null, SR.Arg_InvalidHandle); } object? result = null; @@ -1909,7 +1909,7 @@ private static void GetPropertyOrFieldData( { if (module is null) { - throw new ArgumentNullException(SR.Arg_InvalidHandle); + throw new ArgumentNullException(null, SR.Arg_InvalidHandle); } string? nameLocal = null; diff --git a/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs b/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs index cd98bef0d7d8b..6c4da23b03fe5 100644 --- a/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs +++ b/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs @@ -49,8 +49,7 @@ public TValue this[TKey key] { get { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); Entry entry = Find(key); if (entry == null) @@ -59,8 +58,7 @@ public TValue this[TKey key] } set { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); _version++; Entry entry = Find(key); @@ -73,9 +71,9 @@ public TValue this[TKey key] public bool TryGetValue(TKey key, out TValue? value) { + ArgumentNullException.ThrowIfNull(key); + value = default(TValue); - if (key == null) - throw new ArgumentNullException(nameof(key)); Entry entry = Find(key); if (entry != null) { @@ -87,8 +85,8 @@ public bool TryGetValue(TKey key, out TValue? value) public void Add(TKey key, TValue value) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); + Entry entry = Find(key); if (entry != null) throw new ArgumentException(SR.Format(SR.Argument_AddingDuplicate, key)); @@ -105,8 +103,8 @@ public void Clear(int capacity = DefaultSize) public bool Remove(TKey key) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); + int bucket = GetBucket(key); Entry? prev = null; Entry? entry = _buckets[bucket]; diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs index 7f49242cc5f54..079bd82cf0bf5 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs @@ -111,10 +111,7 @@ public static int GetGeneration(WeakReference wo) object? obj = RuntimeImports.RhHandleGet(wo.WeakHandle); KeepAlive(wo); - if (obj == null) - { - throw new ArgumentNullException(nameof(wo)); - } + ArgumentNullException.ThrowIfNull(obj, nameof(wo)); return RuntimeImports.RhGetGeneration(obj); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs index 0e1ad8d048653..81c9482a55758 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs @@ -348,8 +348,7 @@ public static unsafe void StringBuilderToAnsiString(System.Text.StringBuilder st public static unsafe void AnsiStringToStringBuilder(byte* newBuffer, System.Text.StringBuilder stringBuilder) { - if (newBuffer == null) - throw new ArgumentNullException(nameof(newBuffer)); + ArgumentNullException.ThrowIfNull(newBuffer); int lenAnsi; int lenUnicode; @@ -435,8 +434,7 @@ public static unsafe void WideCharArrayToAnsiCharArray(char[] managedArray, byte return; // Desktop CLR crash (AV at runtime) - we can do better in .NET Native - if (pNative == null) - throw new ArgumentNullException(nameof(pNative)); + ArgumentNullException.ThrowIfNull(pNative); int lenUnicode = managedArray.Length; fixed (char* pManaged = managedArray) diff --git a/src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx b/src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx index b15258417a5b2..5edafe5a5db98 100644 --- a/src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx +++ b/src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx @@ -126,9 +126,6 @@ The key already existed in the dictionary. - - TKey is a reference type and item.Key is null. - The key was of an incorrect type for this dictionary. diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs index 5748725a154a3..8a7afaede743f 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -329,10 +329,7 @@ private void InitializeFromCollection(IEnumerable> co { foreach (KeyValuePair pair in collection) { - if (pair.Key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(pair.Key, "key"); if (!TryAddInternal(_tables, pair.Key, null, pair.Value, updateIfExists: false, acquireLock: false, out _)) { @@ -360,10 +357,7 @@ private void InitializeFromCollection(IEnumerable> co /// The contains too many elements. public bool TryAdd(TKey key, TValue value) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); return TryAddInternal(_tables, key, null, value, updateIfExists: false, acquireLock: true, out _); } @@ -389,10 +383,7 @@ public bool TryAdd(TKey key, TValue value) /// is a null reference (Nothing in Visual Basic). public bool TryRemove(TKey key, [MaybeNullWhen(false)] out TValue value) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); return TryRemoveInternal(key, out value, matchValue: false, default); } @@ -414,10 +405,7 @@ public bool TryRemove(TKey key, [MaybeNullWhen(false)] out TValue value) /// public bool TryRemove(KeyValuePair item) { - if (item.Key is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(item), SR.ConcurrentDictionary_ItemKeyIsNull); - } + ArgumentNullException.ThrowIfNull(item.Key); return TryRemoveInternal(item.Key, out _, matchValue: true, item.Value); } @@ -515,10 +503,7 @@ private bool TryRemoveInternal(TKey key, [MaybeNullWhen(false)] out TValue value /// is a null reference (Nothing in Visual Basic). public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); Tables tables = _tables; @@ -604,10 +589,7 @@ private static bool TryGetValueInternal(Tables tables, TKey key, int hashcode, [ /// is a null reference. public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); return TryUpdateInternal(_tables, key, null, newValue, comparisonValue); } @@ -1098,10 +1080,7 @@ public TValue this[TKey key] } set { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); TryAddInternal(_tables, key, null, value, updateIfExists: true, acquireLock: true, out _); } @@ -1205,15 +1184,8 @@ private int GetCountNoLocks() /// if the key was not in the dictionary. public TValue GetOrAdd(TKey key, Func valueFactory) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - - if (valueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(valueFactory)); - } + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(valueFactory); Tables tables = _tables; @@ -1247,15 +1219,8 @@ public TValue GetOrAdd(TKey key, Func valueFactory) public TValue GetOrAdd(TKey key, Func valueFactory, TArg factoryArgument) where TArg : allows ref struct { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - - if (valueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(valueFactory)); - } + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(valueFactory); Tables tables = _tables; @@ -1284,10 +1249,7 @@ public TValue GetOrAdd(TKey key, Func valueFactory, TA /// key is already in the dictionary, or the new value if the key was not in the dictionary. public TValue GetOrAdd(TKey key, TValue value) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); Tables tables = _tables; @@ -1326,20 +1288,9 @@ public TValue AddOrUpdate( TKey key, Func addValueFactory, Func updateValueFactory, TArg factoryArgument) where TArg : allows ref struct { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - - if (addValueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(addValueFactory)); - } - - if (updateValueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(updateValueFactory)); - } + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); Tables tables = _tables; @@ -1399,20 +1350,9 @@ public TValue AddOrUpdate( /// absent) or the result of updateValueFactory (if the key was present). public TValue AddOrUpdate(TKey key, Func addValueFactory, Func updateValueFactory) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - - if (addValueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(addValueFactory)); - } - - if (updateValueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(updateValueFactory)); - } + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); Tables tables = _tables; @@ -1470,15 +1410,8 @@ public TValue AddOrUpdate(TKey key, Func addValueFactory, Func public TValue AddOrUpdate(TKey key, TValue addValue, Func updateValueFactory) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - - if (updateValueFactory is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(updateValueFactory)); - } + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(updateValueFactory); Tables tables = _tables; @@ -1699,11 +1632,7 @@ bool ICollection>.Remove(KeyValuePair k /// void IDictionary.Add(object key, object? value) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } - + ArgumentNullException.ThrowIfNull(key); if (!(key is TKey)) { throw new ArgumentException(SR.ConcurrentDictionary_TypeOfKeyIncorrect); @@ -1726,10 +1655,7 @@ void IDictionary.Add(object key, object? value) /// (Nothing in Visual Basic). bool IDictionary.Contains(object key) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); return key is TKey tkey && ContainsKey(tkey); } @@ -1777,10 +1703,7 @@ bool IDictionary.Contains(object key) /// (Nothing in Visual Basic). void IDictionary.Remove(object key) { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); if (key is TKey tkey) { @@ -1818,10 +1741,7 @@ void IDictionary.Remove(object key) { get { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); if (key is TKey tkey && TryGetValue(tkey, out TValue? value)) { @@ -1832,10 +1752,7 @@ void IDictionary.Remove(object key) } set { - if (key is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(key); if (!(key is TKey)) { @@ -2491,10 +2408,7 @@ private bool TryAdd(TAlternateKey key, TValue value, bool updateIfExists, out TV } TKey actualKey = comparer.Create(key); - if (actualKey is null) - { - ThrowHelper.ThrowKeyNullException(); - } + ArgumentNullException.ThrowIfNull(actualKey, nameof(key)); // The key was not found in the bucket. Insert the key-value pair. var resultNode = new Node(actualKey, value, hashcode, bucket); @@ -2724,10 +2638,7 @@ internal sealed class IDictionaryDebugView where TKey : notnull public IDictionaryDebugView(IDictionary dictionary) { - if (dictionary is null) - { - ThrowHelper.ThrowArgumentNullException(nameof(dictionary)); - } + ArgumentNullException.ThrowIfNull(dictionary); _dictionary = dictionary; } diff --git a/src/libraries/System.Collections.Concurrent/src/System/ThrowHelper.cs b/src/libraries/System.Collections.Concurrent/src/System/ThrowHelper.cs index bb8a35bac4e41..ea06bf6a3c307 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/ThrowHelper.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/ThrowHelper.cs @@ -8,13 +8,7 @@ namespace System internal static class ThrowHelper { [DoesNotReturn] - internal static void ThrowKeyNullException() => ThrowArgumentNullException("key"); - - [DoesNotReturn] - internal static void ThrowArgumentNullException(string name) => throw new ArgumentNullException(name); - - [DoesNotReturn] - internal static void ThrowArgumentNullException(string name, string message) => throw new ArgumentNullException(name, message); + internal static void ThrowKeyNullException() => throw new ArgumentNullException("key"); [DoesNotReturn] internal static void ThrowValueNullException() => throw new ArgumentException(SR.ConcurrentDictionary_TypeOfValueIncorrect); diff --git a/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs b/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs index c92689a6b034f..500d8b88f9dcc 100644 --- a/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs +++ b/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs @@ -449,7 +449,7 @@ public static void TestRemove3() [Fact] public static void TryRemove_KeyValuePair_ArgumentValidation() { - AssertExtensions.Throws("item", () => new ConcurrentDictionary().TryRemove(new KeyValuePair(null, 42))); + AssertExtensions.Throws("item.Key", () => new ConcurrentDictionary().TryRemove(new KeyValuePair(null, 42))); new ConcurrentDictionary().TryRemove(new KeyValuePair(0, 0)); // no error when using default value type new ConcurrentDictionary().TryRemove(new KeyValuePair(0, 0)); // or nullable } diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs index 78fefca3e0321..01535a3a4f574 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs @@ -107,10 +107,7 @@ public TValue this[TKey key] { get { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); TreeSet>.Node? node = _set.FindNode(new KeyValuePair(key, default(TValue)!)); if (node == null) @@ -122,10 +119,7 @@ public TValue this[TKey key] } set { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); TreeSet>.Node? node = _set.FindNode(new KeyValuePair(key, default(TValue)!)); if (node == null) diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs index d5cd10cb47c4f..9b010bbca060b 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs @@ -266,8 +266,8 @@ void IDictionary.Add(object key, object? value) { ArgumentNullException.ThrowIfNull(key); - if (value == null && default(TValue) != null) // null is an invalid value for Value types - throw new ArgumentNullException(nameof(value)); + if (default(TValue) != null) // null is an invalid value for Value types + ArgumentNullException.ThrowIfNull(value); if (!(key is TKey)) throw new ArgumentException(SR.Format(SR.Arg_WrongType, key, typeof(TKey)), nameof(key)); @@ -579,7 +579,8 @@ public TValue this[TKey key] } set { - if (key == null) throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); + int i = Array.BinarySearch(keys, 0, _size, key, comparer); if (i >= 0) { @@ -613,8 +614,8 @@ public TValue this[TKey key] throw new ArgumentNullException(nameof(key)); } - if (value == null && default(TValue) != null) - throw new ArgumentNullException(nameof(value)); + if (default(TValue) != null) + ArgumentNullException.ThrowIfNull(value); TKey tempKey = (TKey)key; try diff --git a/src/libraries/System.Console/src/System/Console.cs b/src/libraries/System.Console/src/System/Console.cs index d2db8f831d448..1948788067d30 100644 --- a/src/libraries/System.Console/src/System/Console.cs +++ b/src/libraries/System.Console/src/System/Console.cs @@ -528,7 +528,8 @@ public static string Title [UnsupportedOSPlatform("tvos")] set { - ConsolePal.Title = value ?? throw new ArgumentNullException(nameof(value)); + ArgumentNullException.ThrowIfNull(value); + ConsolePal.Title = value; } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs index 050913e6f8684..2fb1796e9ad1c 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs @@ -148,10 +148,7 @@ internal sealed class BrowserHttpController : IDisposable public BrowserHttpController(HttpRequestMessage request, bool? allowAutoRedirect, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request); - if (request.RequestUri == null) - { - throw new ArgumentNullException(nameof(request.RequestUri)); - } + ArgumentNullException.ThrowIfNull(request.RequestUri); _cancellationToken = cancellationToken; _request = request; diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/DSA.Xml.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/DSA.Xml.cs index a0c8cf6afa7a7..92ea198dbb8eb 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/DSA.Xml.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/DSA.Xml.cs @@ -124,15 +124,10 @@ public override string ToXmlString(bool includePrivateParameters) if (includePrivateParameters) { - if (keyParameters.X == null) - { -#pragma warning disable CA2208 // Instantiate argument exceptions correctly - // .NET Framework compat when a 3rd party type lets X be null when - // includePrivateParameters is true - // (the exception would have been from Convert.ToBase64String) - throw new ArgumentNullException("inArray"); -#pragma warning restore CA2208 - } + // .NET Framework compat when a 3rd party type lets X be null when + // includePrivateParameters is true + // (the exception would have been from Convert.ToBase64String) + ArgumentNullException.ThrowIfNull(keyParameters.X, "inArray"); XmlKeyHelper.WriteCryptoBinary(nameof(DSAParameters.X), keyParameters.X, builder); } diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs index 88861c3926d59..5a07b09c2cc17 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs @@ -343,10 +343,7 @@ internal static ReadOnlySpan ArrayToSpanOrThrow( byte[] arg, [CallerArgumentExpression(nameof(arg))] string? paramName = null) { - if (arg is null) - { - throw new ArgumentNullException(paramName); - } + ArgumentNullException.ThrowIfNull(arg, paramName); return arg; } diff --git a/src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs b/src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs index 643ea8590f0cb..23fddeec9c4e9 100644 --- a/src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs +++ b/src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs @@ -20,8 +20,8 @@ public static partial class Parallel public static Task ForAsync(T fromInclusive, T toExclusive, Func body) where T : notnull, IBinaryInteger { - if (fromInclusive is null) throw new ArgumentNullException(nameof(fromInclusive)); - if (toExclusive is null) throw new ArgumentNullException(nameof(toExclusive)); + ArgumentNullException.ThrowIfNull(fromInclusive); + ArgumentNullException.ThrowIfNull(toExclusive); ArgumentNullException.ThrowIfNull(body); return ForAsync(fromInclusive, toExclusive, DefaultDegreeOfParallelism, TaskScheduler.Default, default, body); @@ -38,8 +38,8 @@ public static Task ForAsync(T fromInclusive, T toExclusive, Func(T fromInclusive, T toExclusive, CancellationToken cancellationToken, Func body) where T : notnull, IBinaryInteger { - if (fromInclusive is null) throw new ArgumentNullException(nameof(fromInclusive)); - if (toExclusive is null) throw new ArgumentNullException(nameof(toExclusive)); + ArgumentNullException.ThrowIfNull(fromInclusive); + ArgumentNullException.ThrowIfNull(toExclusive); ArgumentNullException.ThrowIfNull(body); return ForAsync(fromInclusive, toExclusive, DefaultDegreeOfParallelism, TaskScheduler.Default, cancellationToken, body); @@ -56,8 +56,8 @@ public static Task ForAsync(T fromInclusive, T toExclusive, CancellationToken public static Task ForAsync(T fromInclusive, T toExclusive, ParallelOptions parallelOptions, Func body) where T : notnull, IBinaryInteger { - if (fromInclusive is null) throw new ArgumentNullException(nameof(fromInclusive)); - if (toExclusive is null) throw new ArgumentNullException(nameof(toExclusive)); + ArgumentNullException.ThrowIfNull(fromInclusive); + ArgumentNullException.ThrowIfNull(toExclusive); ArgumentNullException.ThrowIfNull(parallelOptions); ArgumentNullException.ThrowIfNull(body); diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/NonWindowsUnsupported.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/NonWindowsUnsupported.cs index 272fa78221d31..025ecc00abb98 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/NonWindowsUnsupported.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/NonWindowsUnsupported.cs @@ -66,12 +66,6 @@ internal OletxTransaction() protected OletxTransaction(SerializationInfo serializationInfo, StreamingContext context) { - //if (serializationInfo == null) - //{ - // throw new ArgumentNullException(nameof(serializationInfo)); - //} - - //throw NotSupported(); throw new PlatformNotSupportedException(); } @@ -116,13 +110,6 @@ internal static IDtcTransaction GetDtcTransaction() public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) { - //if (serializationInfo == null) - //{ - // throw new ArgumentNullException(nameof(serializationInfo)); - //} - - //throw NotSupported(); - throw new PlatformNotSupportedException(); } diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs index f5c325033e25d..af4014e97065f 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs @@ -849,35 +849,6 @@ void ISerializable.GetObjectData( SerializationInfo serializationInfo, StreamingContext context) { - //TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; - //if (etwLog.IsEnabled()) - //{ - // etwLog.MethodEnter(TraceSourceType.TraceSourceLtm, this); - //} - - //ObjectDisposedException.ThrowIf(Disposed, this); - - //if (serializationInfo == null) - //{ - // throw new ArgumentNullException(nameof(serializationInfo)); - //} - - //if (_complete) - //{ - // throw TransactionException.CreateTransactionCompletedException(DistributedTxId); - //} - - //lock (_internalTransaction) - //{ - // _internalTransaction.State.GetObjectData(_internalTransaction, serializationInfo, context); - //} - - //if (etwLog.IsEnabled()) - //{ - // etwLog.TransactionSerialized(this, "Transaction"); - // etwLog.MethodExit(TraceSourceType.TraceSourceLtm, this); - //} - throw new PlatformNotSupportedException(); } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs index 13ba7291b73bf..c891daa7459c8 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs @@ -582,8 +582,7 @@ protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(par for (int i = 0; i < names.Length; i++) { string item = names[i]; - if (item == null) - throw new ArgumentNullException(nameof(names)); + ArgumentNullException.ThrowIfNull(item, nameof(names)); generic_params[i] = new RuntimeGenericTypeParameterBuilder(type, this, item, i); } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs index a091b0dbbff87..537a7df138fcb 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs @@ -613,8 +613,7 @@ protected override PropertyBuilder DefinePropertyCore(string name, PropertyAttri check_name(nameof(name), name); if (parameterTypes != null) foreach (Type param in parameterTypes) - if (param == null) - throw new ArgumentNullException(nameof(parameterTypes)); + ArgumentNullException.ThrowIfNull(param, nameof(parameterTypes)); check_not_created(); RuntimePropertyBuilder res = new RuntimePropertyBuilder(this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);