From a90f60e90ef5d75b69d044ea91360f2b0437882e Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 6 Sep 2016 15:23:37 +0100 Subject: [PATCH] ArraySegment exceptions to ThrowHelper (#7058) --- src/mscorlib/src/System/ArraySegment.cs | 47 +++++++++++++++++---------------- src/mscorlib/src/System/ThrowHelper.cs | 13 +++++++-- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/mscorlib/src/System/ArraySegment.cs b/src/mscorlib/src/System/ArraySegment.cs index d6d26f1..bc39c24 100644 --- a/src/mscorlib/src/System/ArraySegment.cs +++ b/src/mscorlib/src/System/ArraySegment.cs @@ -35,7 +35,7 @@ namespace System public ArraySegment(T[] array) { if (array == null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); _array = array; @@ -46,13 +46,13 @@ namespace System 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; @@ -146,9 +146,9 @@ namespace System 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]; @@ -157,9 +157,9 @@ namespace System 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; @@ -169,7 +169,7 @@ namespace System int IList.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(_array, item, _offset, _count); @@ -182,12 +182,12 @@ namespace System void IList.Insert(int index, T item) { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); } void IList.RemoveAt(int index) { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); } #endregion @@ -197,9 +197,9 @@ namespace System 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]; @@ -220,18 +220,18 @@ namespace System void ICollection.Add(T item) { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); } void ICollection.Clear() { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); } bool ICollection.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(_array, item, _offset, _count); @@ -245,7 +245,7 @@ namespace System void ICollection.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); @@ -253,7 +253,8 @@ namespace System bool ICollection.Remove(T item) { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); + return default(bool); } #endregion @@ -261,7 +262,7 @@ namespace System IEnumerator IEnumerable.GetEnumerator() { if (_array == null) - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray); Contract.EndContractBlock(); return new ArraySegmentEnumerator(this); @@ -272,7 +273,7 @@ namespace System IEnumerator IEnumerable.GetEnumerator() { if (_array == null) - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray")); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray); Contract.EndContractBlock(); return new ArraySegmentEnumerator(this); @@ -314,8 +315,8 @@ namespace System { 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]; } } diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index f327176..a021c9d 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -110,6 +110,11 @@ namespace System { throw new ObjectDisposedException(objectName, Environment.GetResourceString(GetResourceName(resource))); } + internal static void ThrowNotSupportedException() + { + throw new NotSupportedException(); + } + // Allow nulls for reference types and Nullable, 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 @@ -171,7 +176,9 @@ namespace System { view, sourceBytesToCopy, action, - comparison + comparison, + offset, + } // @@ -225,7 +232,9 @@ namespace System { ObjectDisposed_RegKeyClosed, NotSupported_InComparableType, Argument_InvalidRegistryOptionsCheck, - Argument_InvalidRegistryViewCheck + Argument_InvalidRegistryViewCheck, + InvalidOperation_NullArray, + } } -- 2.7.4