-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Collection<T>: Validate parameters in the public methods #23166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,25 @@ public void Insert(int index, T item) | |
InsertItem(index, item); | ||
} | ||
|
||
public void InsertRange(int index, IEnumerable<T> collection) => InsertItemsRange(index, collection); | ||
public void InsertRange(int index, IEnumerable<T> collection) | ||
{ | ||
if (items.IsReadOnly) | ||
{ | ||
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); | ||
} | ||
|
||
if (collection == null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); | ||
} | ||
|
||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exception message is "Index must be within the bounds of the List.". The code consuming this isn't using "List", though. |
||
} | ||
|
||
InsertItemsRange(index, collection); | ||
} | ||
|
||
public bool Remove(T item) | ||
{ | ||
|
@@ -131,9 +149,60 @@ public bool Remove(T item) | |
return true; | ||
} | ||
|
||
public void RemoveRange(int index, int count) => RemoveItemsRange(index, count); | ||
public void RemoveRange(int index, int count) | ||
{ | ||
if (items.IsReadOnly) | ||
{ | ||
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); | ||
} | ||
|
||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRange_IndexException(); | ||
} | ||
|
||
if (count < 0) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); | ||
} | ||
|
||
if (index > items.Count - count) | ||
{ | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception message here is "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection"... but the arguments to this method are "index" and "count", not "offset" and "length"... are we ok with that mismatch? |
||
} | ||
|
||
RemoveItemsRange(index, count); | ||
} | ||
|
||
public void ReplaceRange(int index, int count, IEnumerable<T> collection) | ||
{ | ||
if (items.IsReadOnly) | ||
{ | ||
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); | ||
} | ||
|
||
public void ReplaceRange(int index, int count, IEnumerable<T> collection) => ReplaceItemsRange(index, count, collection); | ||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRange_IndexException(); | ||
} | ||
|
||
if (count < 0) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); | ||
} | ||
|
||
if (index > items.Count - count) | ||
{ | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as earlier. |
||
} | ||
|
||
if (collection == null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as earlier. |
||
} | ||
|
||
ReplaceItemsRange(index, count, collection); | ||
} | ||
|
||
public void RemoveAt(int index) | ||
{ | ||
|
@@ -172,21 +241,6 @@ protected virtual void SetItem(int index, T item) | |
|
||
protected virtual void InsertItemsRange(int index, IEnumerable<T> collection) | ||
{ | ||
if (items.IsReadOnly) | ||
{ | ||
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); | ||
} | ||
|
||
if (collection == null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); | ||
} | ||
|
||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); | ||
} | ||
|
||
if (GetType() == typeof(Collection<T>) && items is List<T> list) | ||
{ | ||
list.InsertRange(index, collection); | ||
|
@@ -202,26 +256,6 @@ protected virtual void InsertItemsRange(int index, IEnumerable<T> collection) | |
|
||
protected virtual void RemoveItemsRange(int index, int count) | ||
{ | ||
if (items.IsReadOnly) | ||
{ | ||
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); | ||
} | ||
|
||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRange_IndexException(); | ||
} | ||
|
||
if (count < 0) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); | ||
} | ||
|
||
if (index > items.Count - count) | ||
{ | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); | ||
} | ||
|
||
if (GetType() == typeof(Collection<T>) && items is List<T> list) | ||
{ | ||
list.RemoveRange(index, count); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no
list
argument to this method, but the exception is going to benew ArgumentNullException("list")
. TheExceptionArgument.list
should be changed toExceptionArgument.collection
.