diff --git a/src/mscorlib/src/System/ArraySegment.cs b/src/mscorlib/src/System/ArraySegment.cs index d6d26f13e5d6..bc39c2474f6b 100644 --- a/src/mscorlib/src/System/ArraySegment.cs +++ b/src/mscorlib/src/System/ArraySegment.cs @@ -35,7 +35,7 @@ public struct ArraySegment : IList, IReadOnlyList public ArraySegment(T[] array) { if (array == null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); _array = array; @@ -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; @@ -146,9 +146,9 @@ T IList.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]; @@ -157,9 +157,9 @@ T IList.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; @@ -169,7 +169,7 @@ T IList.this[int index] 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 @@ int IList.IndexOf(T item) 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 @@ T IReadOnlyList.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]; @@ -220,18 +220,18 @@ bool ICollection.IsReadOnly 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 @@ bool ICollection.Contains(T item) 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 @@ void ICollection.CopyTo(T[] array, int arrayIndex) bool ICollection.Remove(T item) { - throw new NotSupportedException(); + ThrowHelper.ThrowNotSupportedException(); + return default(bool); } #endregion @@ -261,7 +262,7 @@ bool ICollection.Remove(T item) 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 @@ IEnumerator IEnumerable.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); @@ -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]; } } diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index f3271769cc25..a021c9d74089 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -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, 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 @@ internal enum ExceptionArgument { view, sourceBytesToCopy, action, - comparison + comparison, + offset, + } // @@ -225,7 +232,9 @@ internal enum ExceptionResource { ObjectDisposed_RegKeyClosed, NotSupported_InComparableType, Argument_InvalidRegistryOptionsCheck, - Argument_InvalidRegistryViewCheck + Argument_InvalidRegistryViewCheck, + InvalidOperation_NullArray, + } }