Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

ArraySegment<T> exceptions to ThrowHelper #7058

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/mscorlib/src/System/ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
public ArraySegment(T[] array)
{
if (array == null)
throw new ArgumentNullException("array");
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
Contract.EndContractBlock();

_array = array;
Expand All @@ -46,13 +46,13 @@ public ArraySegment(T[] array)
public ArraySegment(T[] array, int offset, int count)
{
if (array == null)
throw new ArgumentNullException("array");
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
if (offset < 0)
throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
if (array.Length - offset < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
Contract.EndContractBlock();

_array = array;
Expand Down Expand Up @@ -146,9 +146,9 @@ T IList<T>.this[int index]
get
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
Contract.EndContractBlock();

return _array[_offset + index];
Expand All @@ -157,9 +157,9 @@ T IList<T>.this[int index]
set
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
Contract.EndContractBlock();

_array[_offset + index] = value;
Expand All @@ -169,7 +169,7 @@ T IList<T>.this[int index]
int IList<T>.IndexOf(T item)
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

int index = System.Array.IndexOf<T>(_array, item, _offset, _count);
Expand All @@ -182,12 +182,12 @@ int IList<T>.IndexOf(T item)

void IList<T>.Insert(int index, T item)
{
throw new NotSupportedException();
ThrowHelper.ThrowNotSupportedException();
}

void IList<T>.RemoveAt(int index)
{
throw new NotSupportedException();
ThrowHelper.ThrowNotSupportedException();
}
#endregion

Expand All @@ -197,9 +197,9 @@ T IReadOnlyList<T>.this[int index]
get
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
Contract.EndContractBlock();

return _array[_offset + index];
Expand All @@ -220,18 +220,18 @@ bool ICollection<T>.IsReadOnly

void ICollection<T>.Add(T item)
{
throw new NotSupportedException();
ThrowHelper.ThrowNotSupportedException();
}

void ICollection<T>.Clear()
{
throw new NotSupportedException();
ThrowHelper.ThrowNotSupportedException();
}

bool ICollection<T>.Contains(T item)
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

int index = System.Array.IndexOf<T>(_array, item, _offset, _count);
Expand All @@ -245,23 +245,24 @@ bool ICollection<T>.Contains(T item)
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

System.Array.Copy(_array, _offset, array, arrayIndex, _count);
}

bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
ThrowHelper.ThrowNotSupportedException();
return default(bool);
}
#endregion

#region IEnumerable<T>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new ArraySegmentEnumerator(this);
Expand All @@ -272,7 +273,7 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
IEnumerator IEnumerable.GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new ArraySegmentEnumerator(this);
Expand Down Expand Up @@ -314,8 +315,8 @@ public T Current
{
get
{
if (_current < _start) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
if (_current >= _end) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
if (_current < _start) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted);
if (_current >= _end) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded);
return _array[_current];
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/mscorlib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ internal static void ThrowObjectDisposedException(string objectName, ExceptionRe
throw new ObjectDisposedException(objectName, Environment.GetResourceString(GetResourceName(resource)));
}

internal static void ThrowNotSupportedException()
{
throw new NotSupportedException();
}

// Allow nulls for reference types and Nullable<U>, but not for value types.
// Aggressively inline so the jit evaluates the if in place and either drops the call altogether
// Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
Expand Down Expand Up @@ -171,7 +176,9 @@ internal enum ExceptionArgument {
view,
sourceBytesToCopy,
action,
comparison
comparison,
offset,

}

//
Expand Down Expand Up @@ -225,7 +232,9 @@ internal enum ExceptionResource {
ObjectDisposed_RegKeyClosed,
NotSupported_InComparableType,
Argument_InvalidRegistryOptionsCheck,
Argument_InvalidRegistryViewCheck
Argument_InvalidRegistryViewCheck,
InvalidOperation_NullArray,

}
}