Skip to content

Commit e102672

Browse files
committed
Use ArgumentNullException.ThrowIfNull and ObjectDisposedException.ThrowIf in more places
1 parent 3f51e14 commit e102672

File tree

56 files changed

+133
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+133
-334
lines changed

src/coreclr/System.Private.CoreLib/src/System/Threading/Monitor.CoreCLR.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ public static bool IsEntered(object obj)
150150
[UnsupportedOSPlatform("browser")]
151151
public static bool Wait(object obj, int millisecondsTimeout)
152152
{
153-
if (obj == null)
154-
throw (new ArgumentNullException(nameof(obj)));
153+
ArgumentNullException.ThrowIfNull(obj);
155154
if (millisecondsTimeout < -1)
156155
throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
157156

src/libraries/Common/src/System/Net/WebSockets/WebSocketValidate.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDis
4040
if (currentState == validState)
4141
{
4242
// Ordering is important to maintain .NET 4.5 WebSocket implementation exception behavior.
43-
if (isDisposed)
44-
{
45-
throw new ObjectDisposedException(nameof(WebSocket));
46-
}
47-
43+
ObjectDisposedException.ThrowIf(isDisposed, typeof(WebSocket));
4844
return;
4945
}
5046
}

src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@
159159
<data name="Partitioner_DynamicPartitionsNotSupported" xml:space="preserve">
160160
<value>Dynamic partitions are not supported by this partitioner.</value>
161161
</data>
162-
<data name="PartitionerStatic_CanNotCallGetEnumeratorAfterSourceHasBeenDisposed" xml:space="preserve">
163-
<value>Can not call GetEnumerator on partitions after the source enumerable is disposed</value>
164-
</data>
165162
<data name="PartitionerStatic_CurrentCalledBeforeMoveNext" xml:space="preserve">
166163
<value>MoveNext must be called at least once before calling Current.</value>
167164
</data>

src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/PartitionerStatic.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,15 +605,10 @@ internal InternalPartitionEnumerable(IEnumerator<TSource> sharedReader, bool use
605605

606606
public IEnumerator<KeyValuePair<long, TSource>> GetEnumerator()
607607
{
608-
if (_disposed)
609-
{
610-
throw new ObjectDisposedException(SR.PartitionerStatic_CanNotCallGetEnumeratorAfterSourceHasBeenDisposed);
611-
}
612-
else
613-
{
614-
return new InternalPartitionEnumerator(_sharedReader, _sharedIndex,
615-
_hasNoElementsLeft, _activePartitionCount, this, _useSingleChunking);
616-
}
608+
ObjectDisposedException.ThrowIf(_disposed, this);
609+
610+
return new InternalPartitionEnumerator(_sharedReader, _sharedIndex,
611+
_hasNoElementsLeft, _activePartitionCount, this, _useSingleChunking);
617612
}
618613

619614

src/libraries/System.ComponentModel.TypeConverter/src/MS/Internal/Xml/Linq/ComponentModel/XComponentModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ public IEnumerable<T> this[string expandedName]
524524
{
525525
get
526526
{
527-
if (expandedName == null)
528-
throw new ArgumentNullException(nameof(expandedName));
527+
ArgumentNullException.ThrowIfNull(expandedName);
529528
if (name == null)
530529
{
531530
name = expandedName;
@@ -559,8 +558,7 @@ public T? this[string expandedName]
559558
{
560559
get
561560
{
562-
if (expandedName == null)
563-
throw new ArgumentNullException(nameof(expandedName));
561+
ArgumentNullException.ThrowIfNull(expandedName);
564562
if (name == null)
565563
{
566564
name = expandedName;

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/Design/ServiceContainer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public virtual void AddService(Type serviceType, object serviceInstance, bool pr
7878
// We're going to add this locally. Ensure that the service instance
7979
// is correct.
8080
//
81-
if (serviceType == null) throw new ArgumentNullException(nameof(serviceType));
82-
if (serviceInstance == null) throw new ArgumentNullException(nameof(serviceInstance));
81+
ArgumentNullException.ThrowIfNull(serviceType);
82+
ArgumentNullException.ThrowIfNull(serviceInstance);
8383
if (!(serviceInstance is ServiceCreatorCallback) && !serviceInstance.GetType().IsCOMObject && !serviceType.IsInstanceOfType(serviceInstance))
8484
{
8585
throw new ArgumentException(SR.Format(SR.ErrorInvalidServiceInstance, serviceType.FullName));
@@ -119,8 +119,8 @@ public virtual void AddService(Type serviceType, ServiceCreatorCallback callback
119119
// We're going to add this locally. Ensure that the service instance
120120
// is correct.
121121
//
122-
if (serviceType == null) throw new ArgumentNullException(nameof(serviceType));
123-
if (callback == null) throw new ArgumentNullException(nameof(callback));
122+
ArgumentNullException.ThrowIfNull(serviceType);
123+
ArgumentNullException.ThrowIfNull(callback);
124124

125125
if (Services.ContainsKey(serviceType))
126126
{

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectPropertyDescriptor.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -956,14 +956,8 @@ protected override void OnValueChanged(object? component, EventArgs e)
956956
/// </summary>
957957
public override void RemoveValueChanged(object? component, EventHandler handler)
958958
{
959-
if (component == null)
960-
{
961-
throw new ArgumentNullException(nameof(component));
962-
}
963-
if (handler == null)
964-
{
965-
throw new ArgumentNullException(nameof(handler));
966-
}
959+
ArgumentNullException.ThrowIfNull(component);
960+
ArgumentNullException.ThrowIfNull(handler);
967961

968962
// If there's an event called <propertyname>Changed, we hooked the caller's
969963
// handler directly up to that on the component, so remove it now.

src/libraries/System.Console/src/System/Console.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static Encoding InputEncoding
8787
}
8888
set
8989
{
90-
CheckNonNull(value, nameof(value));
90+
ArgumentNullException.ThrowIfNull(value);
9191

9292
lock (s_syncObject)
9393
{
@@ -127,7 +127,7 @@ public static Encoding OutputEncoding
127127
[UnsupportedOSPlatform("tvos")]
128128
set
129129
{
130-
CheckNonNull(value, nameof(value));
130+
ArgumentNullException.ThrowIfNull(value);
131131

132132
lock (s_syncObject)
133133
{
@@ -705,7 +705,8 @@ public static Stream OpenStandardError(int bufferSize)
705705
[UnsupportedOSPlatform("tvos")]
706706
public static void SetIn(TextReader newIn)
707707
{
708-
CheckNonNull(newIn, nameof(newIn));
708+
ArgumentNullException.ThrowIfNull(newIn);
709+
709710
newIn = SyncTextReader.GetSynchronizedTextReader(newIn);
710711
lock (s_syncObject)
711712
{
@@ -715,7 +716,8 @@ public static void SetIn(TextReader newIn)
715716

716717
public static void SetOut(TextWriter newOut)
717718
{
718-
CheckNonNull(newOut, nameof(newOut));
719+
ArgumentNullException.ThrowIfNull(newOut);
720+
719721
newOut = TextWriter.Synchronized(newOut);
720722
lock (s_syncObject)
721723
{
@@ -726,7 +728,8 @@ public static void SetOut(TextWriter newOut)
726728

727729
public static void SetError(TextWriter newError)
728730
{
729-
CheckNonNull(newError, nameof(newError));
731+
ArgumentNullException.ThrowIfNull(newError);
732+
730733
newError = TextWriter.Synchronized(newError);
731734
lock (s_syncObject)
732735
{
@@ -735,12 +738,6 @@ public static void SetError(TextWriter newError)
735738
}
736739
}
737740

738-
private static void CheckNonNull(object obj, string paramName)
739-
{
740-
if (obj == null)
741-
throw new ArgumentNullException(paramName);
742-
}
743-
744741
//
745742
// Give a hint to the code generator to not inline the common console methods. The console methods are
746743
// not performance critical. It is unnecessary code bloat to have them inlined.

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBytes.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ public long Read(long offset, byte[] buffer, int offsetInBuffer, int count)
297297
throw new SqlNullValueException();
298298

299299
// Validate the arguments
300-
if (buffer == null)
301-
throw new ArgumentNullException(nameof(buffer));
300+
ArgumentNullException.ThrowIfNull(buffer);
302301

303302
if (offset > Length || offset < 0)
304303
throw new ArgumentOutOfRangeException(nameof(offset));
@@ -343,8 +342,7 @@ public void Write(long offset, byte[] buffer, int offsetInBuffer, int count)
343342
else
344343
{
345344
// Validate the arguments
346-
if (buffer == null)
347-
throw new ArgumentNullException(nameof(buffer));
345+
ArgumentNullException.ThrowIfNull(buffer);
348346

349347
if (_rgbBuf == null)
350348
throw new SqlTypeException(SR.SqlMisc_NoBufferMessage);

src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLChars.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ public long Read(long offset, char[] buffer, int offsetInBuffer, int count)
294294
throw new SqlNullValueException();
295295

296296
// Validate the arguments
297-
if (buffer == null)
298-
throw new ArgumentNullException(nameof(buffer));
297+
ArgumentNullException.ThrowIfNull(buffer);
299298

300299
if (offset > Length || offset < 0)
301300
throw new ArgumentOutOfRangeException(nameof(offset));
@@ -341,8 +340,7 @@ public void Write(long offset, char[] buffer, int offsetInBuffer, int count)
341340
else
342341
{
343342
// Validate the arguments
344-
if (buffer == null)
345-
throw new ArgumentNullException(nameof(buffer));
343+
ArgumentNullException.ThrowIfNull(buffer);
346344

347345
if (_rgchBuf == null)
348346
throw new SqlTypeException(SR.SqlMisc_NoBufferMessage);
@@ -650,8 +648,7 @@ public int Read(char[] buffer, int offset, int count)
650648
{
651649
CheckIfStreamClosed();
652650

653-
if (buffer == null)
654-
throw new ArgumentNullException(nameof(buffer));
651+
ArgumentNullException.ThrowIfNull(buffer);
655652
if (offset < 0 || offset > buffer.Length)
656653
throw new ArgumentOutOfRangeException(nameof(offset));
657654
if (count < 0 || count > buffer.Length - offset)
@@ -667,8 +664,7 @@ public void Write(char[] buffer, int offset, int count)
667664
{
668665
CheckIfStreamClosed();
669666

670-
if (buffer == null)
671-
throw new ArgumentNullException(nameof(buffer));
667+
ArgumentNullException.ThrowIfNull(buffer);
672668
if (offset < 0 || offset > buffer.Length)
673669
throw new ArgumentOutOfRangeException(nameof(offset));
674670
if (count < 0 || count > buffer.Length - offset)

0 commit comments

Comments
 (0)