Updated Exception Handling for Collection<T> (#23290)
authorAndrew Hoefling <andrew@hoeflingsoftware.com>
Fri, 22 Mar 2019 02:23:53 +0000 (22:23 -0400)
committerStephen Toub <stoub@microsoft.com>
Fri, 22 Mar 2019 02:23:53 +0000 (22:23 -0400)
* Updated Argument Helper param from list->collection since the parameter name is collection

* Updated exception message to use an out of range exception that doesn't explicitly reference a list

* Simplified if statements that verify if the index is out of range

* Updated if logic to be simplified using (uint)

* Updated exception handling to throw ThrowHelper.ThrowArgumentOutOfRange_IndexException() when the ExceptionArgument was 'Index'

src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs

index 5d6c9c9..4898c0c 100644 (file)
@@ -49,7 +49,7 @@ namespace System.Collections.ObjectModel
                     ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
                 }
 
-                if (index < 0 || index >= items.Count)
+                if ((uint)index >= (uint)items.Count)
                 {
                     ThrowHelper.ThrowArgumentOutOfRange_IndexException();
                 }
@@ -108,9 +108,9 @@ namespace System.Collections.ObjectModel
                 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
             }
 
-            if (index < 0 || index > items.Count)
+            if ((uint)index > (uint)items.Count)
             {
-                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+                ThrowHelper.ThrowArgumentOutOfRange_IndexException();
             }
 
             InsertItem(index, item);
@@ -125,12 +125,12 @@ namespace System.Collections.ObjectModel
 
             if (collection == null)
             {
-                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
+                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
             }
 
             if ((uint)index > (uint)items.Count)
             {
-                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+                ThrowHelper.ThrowArgumentOutOfRange_IndexException();
             }
 
             InsertItemsRange(index, collection);
@@ -198,7 +198,7 @@ namespace System.Collections.ObjectModel
 
             if (collection == null)
             {
-                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
+                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
             }
 
             ReplaceItemsRange(index, count, collection);
@@ -211,7 +211,7 @@ namespace System.Collections.ObjectModel
                 ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
             }
 
-            if (index < 0 || index >= items.Count)
+            if ((uint)index >= (uint)items.Count)
             {
                 ThrowHelper.ThrowArgumentOutOfRange_IndexException();
             }