diff --git a/src/mscorlib/src/System/Array.cs b/src/mscorlib/src/System/Array.cs index a97ce9d53301..08790490f479 100644 --- a/src/mscorlib/src/System/Array.cs +++ b/src/mscorlib/src/System/Array.cs @@ -34,7 +34,7 @@ internal Array() {} public static ReadOnlyCollection AsReadOnly(T[] array) { if (array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result>() != null); @@ -45,7 +45,7 @@ public static ReadOnlyCollection AsReadOnly(T[] array) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static void Resize(ref T[] array, int newSize) { if (newSize < 0) - throw new ArgumentOutOfRangeException("newSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.newSize, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); Contract.Ensures(Contract.ValueAtReturn(out array) != null); Contract.Ensures(Contract.ValueAtReturn(out array).Length == newSize); Contract.EndContractBlock(); @@ -68,9 +68,9 @@ public static void Resize(ref T[] array, int newSize) { public unsafe static Array CreateInstance(Type elementType, int length) { if ((object)elementType == null) - throw new ArgumentNullException("elementType"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); if (length < 0) - throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Length == length); Contract.Ensures(Contract.Result().Rank == 1); @@ -78,7 +78,7 @@ public unsafe static Array CreateInstance(Type elementType, int length) RuntimeType t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"elementType"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType); return InternalCreate((void*)t.TypeHandle.Value,1,&length,null); } @@ -86,9 +86,11 @@ public unsafe static Array CreateInstance(Type elementType, int length) public unsafe static Array CreateInstance(Type elementType, int length1, int length2) { if ((object)elementType == null) - throw new ArgumentNullException("elementType"); - if (length1 < 0 || length2 < 0) - throw new ArgumentOutOfRangeException((length1 < 0 ? "length1" : "length2"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); + if (length1 < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length1, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + if (length2 < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length2, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Rank == 2); Contract.Ensures(Contract.Result().GetLength(0) == length1); @@ -96,7 +98,7 @@ public unsafe static Array CreateInstance(Type elementType, int length1, int len RuntimeType t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"elementType"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType); int* pLengths = stackalloc int[2]; pLengths[0] = length1; pLengths[1] = length2; @@ -107,13 +109,13 @@ public unsafe static Array CreateInstance(Type elementType, int length1, int len public unsafe static Array CreateInstance(Type elementType, int length1, int length2, int length3) { if ((object)elementType == null) - throw new ArgumentNullException("elementType"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); if (length1 < 0) - throw new ArgumentOutOfRangeException("length1", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length1, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); if (length2 < 0) - throw new ArgumentOutOfRangeException("length2", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length2, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); if (length3 < 0) - throw new ArgumentOutOfRangeException("length3", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length3, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Rank == 3); Contract.Ensures(Contract.Result().GetLength(0) == length1); @@ -122,7 +124,7 @@ public unsafe static Array CreateInstance(Type elementType, int length1, int len RuntimeType t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "elementType"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType); int* pLengths = stackalloc int[3]; pLengths[0] = length1; pLengths[1] = length2; @@ -134,38 +136,38 @@ public unsafe static Array CreateInstance(Type elementType, int length1, int len public unsafe static Array CreateInstance(Type elementType, params int[] lengths) { if ((object)elementType == null) - throw new ArgumentNullException("elementType"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); if (lengths == null) - throw new ArgumentNullException("lengths"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lengths); if (lengths.Length == 0) - throw new ArgumentException(Environment.GetResourceString("Arg_NeedAtLeast1Rank")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NeedAtLeast1Rank); Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Rank == lengths.Length); Contract.EndContractBlock(); RuntimeType t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "elementType"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType); // Check to make sure the lenghts are all positive. Note that we check this here to give // a good exception message if they are not; however we check this again inside the execution // engine's low level allocation function after having made a copy of the array to prevent a // malicious caller from mutating the array after this check. - for (int i=0;i() != null); Contract.Ensures(Contract.Result().Rank == lengths.Length); Contract.EndContractBlock(); @@ -175,8 +177,8 @@ public static Array CreateInstance(Type elementType, params long[] lengths) for (int i = 0; i < lengths.Length; ++i) { long len = lengths[i]; - if (len > Int32.MaxValue || len < Int32.MinValue) - throw new ArgumentOutOfRangeException("len", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (len > Int32.MaxValue || len < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.len, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); intLengths[i] = (int) len; } @@ -188,22 +190,22 @@ public static Array CreateInstance(Type elementType, params long[] lengths) public unsafe static Array CreateInstance(Type elementType, int[] lengths,int[] lowerBounds) { if (elementType == null) - throw new ArgumentNullException("elementType"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType); if (lengths == null) - throw new ArgumentNullException("lengths"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lengths); if (lowerBounds == null) - throw new ArgumentNullException("lowerBounds"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lowerBounds); if (lengths.Length != lowerBounds.Length) - throw new ArgumentException(Environment.GetResourceString("Arg_RanksAndBounds")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RanksAndBounds); if (lengths.Length == 0) - throw new ArgumentException(Environment.GetResourceString("Arg_NeedAtLeast1Rank")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NeedAtLeast1Rank); Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Rank == lengths.Length); Contract.EndContractBlock(); RuntimeType t = elementType.UnderlyingSystemType as RuntimeType; if (t == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "elementType"); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType); // Check to make sure the lenghts are all positive. Note that we check this here to give // a good exception message if they are not; however we check this again inside the execution @@ -211,9 +213,9 @@ public unsafe static Array CreateInstance(Type elementType, int[] lengths,int[] // malicious caller from mutating the array after this check. for (int i=0;i= 0); @@ -313,8 +315,8 @@ public static void ConstrainedCopy(Array sourceArray, int sourceIndex, Array des [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Copy(Array sourceArray, Array destinationArray, long length) { - if (length > Int32.MaxValue || length < Int32.MinValue) - throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (length > Int32.MaxValue || length < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Array.Copy(sourceArray, destinationArray, (int) length); } @@ -322,12 +324,12 @@ public static void Copy(Array sourceArray, Array destinationArray, long length) [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length) { - if (sourceIndex > Int32.MaxValue || sourceIndex < Int32.MinValue) - throw new ArgumentOutOfRangeException("sourceIndex", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (destinationIndex > Int32.MaxValue || destinationIndex < Int32.MinValue) - throw new ArgumentOutOfRangeException("destinationIndex", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (length > Int32.MaxValue || length < Int32.MinValue) - throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (sourceIndex > Int32.MaxValue || sourceIndex < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.sourceIndex, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (destinationIndex > Int32.MaxValue || destinationIndex < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destinationIndex, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (length > Int32.MaxValue || length < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Array.Copy(sourceArray, (int) sourceIndex, destinationArray, (int) destinationIndex, (int) length); } @@ -346,9 +348,9 @@ public static void Copy(Array sourceArray, long sourceIndex, Array destinationAr public unsafe Object GetValue(params int[] indices) { if (indices == null) - throw new ArgumentNullException("indices"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices); if (Rank != indices.Length) - throw new ArgumentException(Environment.GetResourceString("Arg_RankIndices")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices); Contract.EndContractBlock(); TypedReference elemref = new TypedReference(); @@ -361,7 +363,7 @@ public unsafe Object GetValue(params int[] indices) public unsafe Object GetValue(int index) { if (Rank != 1) - throw new ArgumentException(Environment.GetResourceString("Arg_Need1DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); Contract.EndContractBlock(); TypedReference elemref = new TypedReference(); @@ -373,7 +375,7 @@ public unsafe Object GetValue(int index) public unsafe Object GetValue(int index1, int index2) { if (Rank != 2) - throw new ArgumentException(Environment.GetResourceString("Arg_Need2DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need2DArray); Contract.EndContractBlock(); int* pIndices = stackalloc int[2]; @@ -389,7 +391,7 @@ public unsafe Object GetValue(int index1, int index2) public unsafe Object GetValue(int index1, int index2, int index3) { if (Rank != 3) - throw new ArgumentException(Environment.GetResourceString("Arg_Need3DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need3DArray); Contract.EndContractBlock(); int* pIndices = stackalloc int[3]; @@ -405,8 +407,8 @@ public unsafe Object GetValue(int index1, int index2, int index3) [ComVisible(false)] public Object GetValue(long index) { - if (index > Int32.MaxValue || index < Int32.MinValue) - throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index > Int32.MaxValue || index < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); return this.GetValue((int) index); @@ -415,10 +417,10 @@ public Object GetValue(long index) [ComVisible(false)] public Object GetValue(long index1, long index2) { - if (index1 > Int32.MaxValue || index1 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index1", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index2 > Int32.MaxValue || index2 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index2", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index1 > Int32.MaxValue || index1 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index2 > Int32.MaxValue || index2 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); return this.GetValue((int) index1, (int) index2); @@ -427,12 +429,12 @@ public Object GetValue(long index1, long index2) [ComVisible(false)] public Object GetValue(long index1, long index2, long index3) { - if (index1 > Int32.MaxValue || index1 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index1", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index2 > Int32.MaxValue || index2 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index2", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index3 > Int32.MaxValue || index3 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index3", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index1 > Int32.MaxValue || index1 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index2 > Int32.MaxValue || index2 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index3 > Int32.MaxValue || index3 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index3, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); return this.GetValue((int) index1, (int) index2, (int) index3); @@ -442,9 +444,9 @@ public Object GetValue(long index1, long index2, long index3) public Object GetValue(params long[] indices) { if (indices == null) - throw new ArgumentNullException("indices"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices); if (Rank != indices.Length) - throw new ArgumentException(Environment.GetResourceString("Arg_RankIndices")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices); Contract.EndContractBlock(); int[] intIndices = new int[indices.Length]; @@ -452,8 +454,8 @@ public Object GetValue(params long[] indices) for (int i = 0; i < indices.Length; ++i) { long index = indices[i]; - if (index > Int32.MaxValue || index < Int32.MinValue) - throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index > Int32.MaxValue || index < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); intIndices[i] = (int) index; } @@ -465,7 +467,7 @@ public Object GetValue(params long[] indices) public unsafe void SetValue(Object value,int index) { if (Rank != 1) - throw new ArgumentException(Environment.GetResourceString("Arg_Need1DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need1DArray); Contract.EndContractBlock(); TypedReference elemref = new TypedReference(); @@ -477,7 +479,7 @@ public unsafe void SetValue(Object value,int index) public unsafe void SetValue(Object value,int index1, int index2) { if (Rank != 2) - throw new ArgumentException(Environment.GetResourceString("Arg_Need2DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need2DArray); Contract.EndContractBlock(); int* pIndices = stackalloc int[2]; @@ -493,7 +495,7 @@ public unsafe void SetValue(Object value,int index1, int index2) public unsafe void SetValue(Object value,int index1, int index2, int index3) { if (Rank != 3) - throw new ArgumentException(Environment.GetResourceString("Arg_Need3DArray")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need3DArray); Contract.EndContractBlock(); int* pIndices = stackalloc int[3]; @@ -510,9 +512,9 @@ public unsafe void SetValue(Object value,int index1, int index2, int index3) public unsafe void SetValue(Object value,params int[] indices) { if (indices == null) - throw new ArgumentNullException("indices"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices); if (Rank != indices.Length) - throw new ArgumentException(Environment.GetResourceString("Arg_RankIndices")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices); Contract.EndContractBlock(); TypedReference elemref = new TypedReference(); @@ -524,8 +526,8 @@ public unsafe void SetValue(Object value,params int[] indices) [ComVisible(false)] public void SetValue(Object value, long index) { - if (index > Int32.MaxValue || index < Int32.MinValue) - throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index > Int32.MaxValue || index < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); this.SetValue(value, (int) index); @@ -534,10 +536,10 @@ public void SetValue(Object value, long index) [ComVisible(false)] public void SetValue(Object value, long index1, long index2) { - if (index1 > Int32.MaxValue || index1 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index1", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index2 > Int32.MaxValue || index2 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index2", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index1 > Int32.MaxValue || index1 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index2 > Int32.MaxValue || index2 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); this.SetValue(value, (int) index1, (int) index2); @@ -546,12 +548,12 @@ public void SetValue(Object value, long index1, long index2) [ComVisible(false)] public void SetValue(Object value, long index1, long index2, long index3) { - if (index1 > Int32.MaxValue || index1 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index1", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index2 > Int32.MaxValue || index2 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index2", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); - if (index3 > Int32.MaxValue || index3 < Int32.MinValue) - throw new ArgumentOutOfRangeException("index3", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index1 > Int32.MaxValue || index1 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index1, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index2 > Int32.MaxValue || index2 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index2, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); + if (index3 > Int32.MaxValue || index3 < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index3, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); this.SetValue(value, (int) index1, (int) index2, (int) index3); @@ -561,9 +563,9 @@ public void SetValue(Object value, long index1, long index2, long index3) public void SetValue(Object value, params long[] indices) { if (indices == null) - throw new ArgumentNullException("indices"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.indices); if (Rank != indices.Length) - throw new ArgumentException(Environment.GetResourceString("Arg_RankIndices")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankIndices); Contract.EndContractBlock(); int[] intIndices = new int[indices.Length]; @@ -571,8 +573,8 @@ public void SetValue(Object value, params long[] indices) for (int i = 0; i < indices.Length; ++i) { long index = indices[i]; - if (index > Int32.MaxValue || index < Int32.MinValue) - throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index > Int32.MaxValue || index < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); intIndices[i] = (int) index; } @@ -693,7 +695,8 @@ Object IList.this[int index] { int IList.Add(Object value) { - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); + return default(int); } bool IList.Contains(Object value) @@ -713,17 +716,17 @@ int IList.IndexOf(Object value) void IList.Insert(int index, Object value) { - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } void IList.Remove(Object value) { - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } void IList.RemoveAt(int index) { - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } // Make a new array which is a shallow copy of the original array. @@ -740,8 +743,9 @@ Int32 IStructuralComparable.CompareTo(Object other, IComparer comparer) { Array o = other as Array; - if (o == null || this.Length != o.Length) { - throw new ArgumentException(Environment.GetResourceString("ArgumentException_OtherNotArrayOfCorrectLength"), "other"); + if (o == null || this.Length != o.Length) + { + ThrowHelper.ThrowArgumentException(ExceptionResource.ArgumentException_OtherNotArrayOfCorrectLength, ExceptionArgument.other); } int i = 0; @@ -795,7 +799,7 @@ internal static int CombineHashCodes(int h1, int h2) { int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { if (comparer == null) - throw new ArgumentNullException("comparer"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer); Contract.EndContractBlock(); int ret = 0; @@ -824,7 +828,7 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int BinarySearch(Array array, Object value) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures((Contract.Result() >= array.GetLowerBound(0) && Contract.Result() <= array.GetUpperBound(0)) || (Contract.Result() < array.GetLowerBound(0) && ~Contract.Result() <= array.GetUpperBound(0) + 1)); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -868,7 +872,7 @@ public static int BinarySearch(Array array, int index, int length, Object value) [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int BinarySearch(Array array, Object value, IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); return BinarySearch(array, lb, array.Length, value, comparer); @@ -894,15 +898,17 @@ public static int BinarySearch(Array array, Object value, IComparer comparer) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int BinarySearch(Array array, int index, int length, Object value, IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); - if (index < lb || length < 0) - throw new ArgumentOutOfRangeException((index(T[] array, T value) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); return BinarySearch(array, 0, array.Length, value, null); } @@ -977,7 +983,7 @@ public static int BinarySearch(T[] array, T value) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int BinarySearch(T[] array, T value, System.Collections.Generic.IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); return BinarySearch(array, 0, array.Length, value, comparer); } @@ -992,11 +998,14 @@ public static int BinarySearch(T[] array, int index, int length, T value) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int BinarySearch(T[] array, int index, int length, T value, System.Collections.Generic.IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); - if (index < 0 || length < 0) - throw new ArgumentOutOfRangeException((index<0 ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + if (index < 0) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + if (array.Length - index < length) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); return ArraySortHelper.Default.BinarySearch(array, index, length, value, comparer); @@ -1004,11 +1013,11 @@ public static int BinarySearch(T[] array, int index, int length, T value, Sys public static TOutput[] ConvertAll(TInput[] array, Converter converter) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( converter == null) { - throw new ArgumentNullException("converter"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter); } Contract.Ensures(Contract.Result() != null); Contract.Ensures(Contract.Result().Length == array.Length); @@ -1032,7 +1041,7 @@ public static TOutput[] ConvertAll(TInput[] array, Converter Int32.MaxValue || index < Int32.MinValue) - throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_HugeArrayNotSupported")); + if (index > Int32.MaxValue || index < Int32.MinValue) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported); Contract.EndContractBlock(); this.CopyTo(array, (int) index); @@ -1066,11 +1075,11 @@ public static bool Exists(T[] array, Predicate match) { public static T Find(T[] array, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); @@ -1084,11 +1093,11 @@ public static T Find(T[] array, Predicate match) { public static T[] FindAll(T[] array, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); @@ -1103,7 +1112,7 @@ public static T[] FindAll(T[] array, Predicate match) { public static int FindIndex(T[] array, Predicate match) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1113,7 +1122,7 @@ public static int FindIndex(T[] array, Predicate match) { public static int FindIndex(T[] array, int startIndex, Predicate match) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1123,19 +1132,19 @@ public static int FindIndex(T[] array, int startIndex, Predicate match) { public static int FindIndex(T[] array, int startIndex, int count, Predicate match) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( startIndex < 0 || startIndex > array.Length ) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (count < 0 || startIndex > array.Length - count) { - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1149,11 +1158,11 @@ public static int FindIndex(T[] array, int startIndex, int count, Predicate(T[] array, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); @@ -1167,7 +1176,7 @@ public static T FindLast(T[] array, Predicate match) { public static int FindLastIndex(T[] array, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.EndContractBlock(); @@ -1176,7 +1185,7 @@ public static int FindLastIndex(T[] array, Predicate match) { public static int FindLastIndex(T[] array, int startIndex, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.EndContractBlock(); @@ -1185,30 +1194,30 @@ public static int FindLastIndex(T[] array, int startIndex, Predicate match public static int FindLastIndex(T[] array, int startIndex, int count, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); if(array.Length == 0) { // Special case for 0 length List if( startIndex != -1) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } } else { // Make sure we're not out of range if ( startIndex < 0 || startIndex >= array.Length) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } } // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0. if (count < 0 || startIndex - count + 1 < 0) { - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } int endIndex = startIndex - count; @@ -1222,11 +1231,11 @@ public static int FindLastIndex(T[] array, int startIndex, int count, Predica public static void ForEach(T[] array, Action action) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( action == null) { - throw new ArgumentNullException("action"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action); } Contract.EndContractBlock(); @@ -1255,7 +1264,7 @@ public IEnumerator GetEnumerator() [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int IndexOf(Array array, Object value) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -1271,7 +1280,7 @@ public static int IndexOf(Array array, Object value) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int IndexOf(Array array, Object value, int startIndex) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -1288,17 +1297,17 @@ public static int IndexOf(Array array, Object value, int startIndex) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int IndexOf(Array array, Object value, int startIndex, int count) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); if (array.Rank != 1) - throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); + ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); if (startIndex < lb || startIndex > array.Length + lb) - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (count < 0 || count > array.Length - startIndex + lb) - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); // Try calling a quick native method to handle primitive types. int retVal; @@ -1342,7 +1351,7 @@ public static int IndexOf(Array array, Object value, int startIndex, int count) [Pure] public static int IndexOf(T[] array, T value) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures((Contract.Result() < 0) || (Contract.Result() >= 0 && Contract.Result() < array.Length && EqualityComparer.Default.Equals(value, array[Contract.Result()]))); @@ -1353,7 +1362,7 @@ public static int IndexOf(T[] array, T value) { public static int IndexOf(T[] array, T value, int startIndex) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1363,15 +1372,15 @@ public static int IndexOf(T[] array, T value, int startIndex) { public static int IndexOf(T[] array, T value, int startIndex, int count) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (startIndex < 0 || startIndex > array.Length ) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } if (count < 0 || count > array.Length - startIndex) { - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1392,7 +1401,7 @@ public static int IndexOf(T[] array, T value, int startIndex, int count) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int LastIndexOf(Array array, Object value) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -1407,7 +1416,7 @@ public static int LastIndexOf(Array array, Object value) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int LastIndexOf(Array array, Object value, int startIndex) { if (array == null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -1424,7 +1433,7 @@ public static int LastIndexOf(Array array, Object value, int startIndex) { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public static int LastIndexOf(Array array, Object value, int startIndex, int count) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.Ensures(Contract.Result() < array.GetLowerBound(0) + array.Length); Contract.EndContractBlock(); int lb = array.GetLowerBound(0); @@ -1433,13 +1442,13 @@ public static int LastIndexOf(Array array, Object value, int startIndex, int cou } if (startIndex < lb || startIndex >= array.Length + lb) - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (count < 0) - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); if (count > startIndex - lb + 1) - throw new ArgumentOutOfRangeException("endIndex", Environment.GetResourceString("ArgumentOutOfRange_EndIndexStartIndex")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.endIndex, ExceptionResource.ArgumentOutOfRange_EndIndexStartIndex); if (array.Rank != 1) - throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); + ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported); // Try calling a quick native method to handle primitive types. int retVal; @@ -1478,7 +1487,7 @@ public static int LastIndexOf(Array array, Object value, int startIndex, int cou public static int LastIndexOf(T[] array, T value) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1488,7 +1497,7 @@ public static int LastIndexOf(T[] array, T value) { public static int LastIndexOf(T[] array, T value, int startIndex) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1498,7 +1507,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex) { public static int LastIndexOf(T[] array, T value, int startIndex, int count) { if (array==null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } Contract.Ensures(Contract.Result() < array.Length); Contract.EndContractBlock(); @@ -1509,24 +1518,24 @@ public static int LastIndexOf(T[] array, T value, int startIndex, int count) // accept -1 and 0 as valid startIndex for compablility reason. // if( startIndex != -1 && startIndex != 0) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } // only 0 is a valid value for count if array is empty if( count != 0) { - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } return -1; } // Make sure we're not out of range if ( startIndex < 0 || startIndex >= array.Length) { - throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); } // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0. if (count < 0 || startIndex - count + 1 < 0) { - throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count")); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count); } return EqualityComparer.Default.LastIndexOf(array, value, startIndex, count); @@ -1546,7 +1555,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex, int count) [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Reverse(Array array) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); Reverse(array, array.GetLowerBound(0), array.Length); } @@ -1561,14 +1570,17 @@ public static void Reverse(Array array) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Reverse(Array array, int index, int length) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); int lowerBound = array.GetLowerBound(0); - if (index < lowerBound || length < 0) - throw new ArgumentOutOfRangeException((index<0 ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (index < lowerBound) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + if (array.Length - (index - lowerBound) < length) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); if (array.Rank != 1) - throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); + ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported); Contract.EndContractBlock(); bool r = TrySZReverse(array, index, length); @@ -1610,7 +1622,7 @@ public static void Reverse(Array array, int index, int length) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(Array array) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); Sort(array, null, array.GetLowerBound(0), array.Length, null); } @@ -1624,7 +1636,7 @@ public static void Sort(Array array) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(Array keys, Array items) { if (keys==null) - throw new ArgumentNullException("keys"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); Contract.EndContractBlock(); Sort(keys, items, keys.GetLowerBound(0), keys.Length, null); } @@ -1658,7 +1670,7 @@ public static void Sort(Array keys, Array items, int index, int length) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(Array array, IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); Sort(array, null, array.GetLowerBound(0), array.Length, comparer); } @@ -1674,7 +1686,7 @@ public static void Sort(Array array, IComparer comparer) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(Array keys, Array items, IComparer comparer) { if (keys==null) - throw new ArgumentNullException("keys"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); Contract.EndContractBlock(); Sort(keys, items, keys.GetLowerBound(0), keys.Length, comparer); } @@ -1702,16 +1714,19 @@ public static void Sort(Array array, int index, int length, IComparer comparer) [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(Array keys, Array items, int index, int length, IComparer comparer) { if (keys==null) - throw new ArgumentNullException("keys"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); if (keys.Rank != 1 || (items != null && items.Rank != 1)) - throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); + ThrowHelper.ThrowRankException(ExceptionResource.Rank_MultiDimNotSupported); int keysLowerBound = keys.GetLowerBound(0); if (items != null && keysLowerBound != items.GetLowerBound(0)) - throw new ArgumentException(Environment.GetResourceString("Arg_LowerBoundsMustMatch")); - if (index < keysLowerBound || length < 0) - throw new ArgumentOutOfRangeException((length<0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_LowerBoundsMustMatch); + if (index < keysLowerBound) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + if (keys.Length - (index - keysLowerBound) < length || (items != null && (index - keysLowerBound) > items.Length - length)) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); @@ -1745,7 +1760,7 @@ public static void Sort(Array keys, Array items, int index, int length, ICompare [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(T[] array) { if (array==null) - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); Sort(array, 0, array.Length, null); } @@ -1753,7 +1768,7 @@ public static void Sort(T[] array) { [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(TKey[] keys, TValue[] items) { if (keys==null) - throw new ArgumentNullException("keys"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); Contract.EndContractBlock(); Sort(keys, items, 0, keys.Length, null); } @@ -1770,8 +1785,8 @@ public static void Sort(TKey[] keys, TValue[] items, int index, in [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(T[] array, System.Collections.Generic.IComparer comparer) { - if (array==null) - throw new ArgumentNullException("array"); + if (array == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); Contract.EndContractBlock(); Sort(array, 0, array.Length, comparer); } @@ -1779,7 +1794,7 @@ public static void Sort(T[] array, System.Collections.Generic.IComparer co [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(TKey[] keys, TValue[] items, System.Collections.Generic.IComparer comparer) { if (keys==null) - throw new ArgumentNullException("keys"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); Contract.EndContractBlock(); Sort(keys, items, 0, keys.Length, comparer); } @@ -1788,11 +1803,13 @@ public static void Sort(TKey[] keys, TValue[] items, System.Collec [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(T[] array, int index, int length, System.Collections.Generic.IComparer comparer) { if (array==null) - throw new ArgumentNullException("array"); - if (index < 0 || length < 0) - throw new ArgumentOutOfRangeException((length<0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + if (index < 0) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); if (array.Length - index < length) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); if (length > 1) { @@ -1810,11 +1827,13 @@ public static void Sort(T[] array, int index, int length, System.Collections. [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)] public static void Sort(TKey[] keys, TValue[] items, int index, int length, System.Collections.Generic.IComparer comparer) { if (keys==null) - throw new ArgumentNullException("keys"); - if (index < 0 || length < 0) - throw new ArgumentOutOfRangeException((length<0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keys); + if (index < 0) + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); + if (length < 0) + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); if (keys.Length - index < length || (items != null && index > items.Length - length)) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); if (length > 1) { @@ -1836,11 +1855,11 @@ public static void Sort(TKey[] keys, TValue[] items, int index, in public static void Sort(T[] array, Comparison comparison) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( comparison == null) { - throw new ArgumentNullException("comparison"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison); } Contract.EndContractBlock(); @@ -1850,11 +1869,11 @@ public static void Sort(T[] array, Comparison comparison) { public static bool TrueForAll(T[] array, Predicate match) { if( array == null) { - throw new ArgumentNullException("array"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if( match == null) { - throw new ArgumentNullException("match"); + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); @@ -1950,11 +1969,11 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (IndexOutOfRangeException) { - throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", comparer)); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_BogusIComparer, ExceptionArgument.comparer); } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } } @@ -1976,7 +1995,7 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } Object x = keys[middle]; do @@ -1990,11 +2009,11 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (IndexOutOfRangeException) { - throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", comparer)); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_BogusIComparer, ExceptionArgument.comparer); } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } Contract.Assert(i >= left && j <= right, "(i>=left && j<=right) Sort failed - Is your IComparer bogus?"); if (i > j) break; @@ -2047,7 +2066,7 @@ private void IntrospectiveSort(int left, int length) } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } } @@ -2269,11 +2288,11 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (IndexOutOfRangeException) { - throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", comparer)); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_BogusIComparer, ExceptionArgument.comparer); } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } } @@ -2292,7 +2311,7 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } Object x = keys.GetValue(middle); @@ -2307,11 +2326,11 @@ private void DepthLimitedQuickSort(int left, int right, int depthLimit) } catch (IndexOutOfRangeException) { - throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", comparer)); + ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_BogusIComparer, ExceptionArgument.comparer); } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } Contract.Assert(i >= left && j <= right, "(i>=left && j<=right) Sort failed - Is your IComparer bogus?"); if (i > j) break; @@ -2364,7 +2383,7 @@ private void IntrospectiveSort(int left, int length) } catch (Exception e) { - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), e); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e); } } @@ -2532,8 +2551,8 @@ public bool MoveNext() { public Object Current { get { - if (_index < 0) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted)); - if (_index >= _endIndex) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded)); + if (_index < 0) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted); + if (_index >= _endIndex) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded); return _array.GetValue(_index); } } @@ -2610,8 +2629,8 @@ public bool MoveNext() { public Object Current { get { - if (index < startIndex) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted)); - if (_complete) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded)); + if (index < startIndex) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted); + if (_complete) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded); return array.GetValue(_indices); } } @@ -2734,7 +2753,7 @@ internal void set_Item(int index, T value) { void Add(T value) { // Not meaningful for arrays. - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } [SecuritySafeCritical] @@ -2752,7 +2771,8 @@ bool get_IsReadOnly() { void Clear() { //! Warning: "this" is an array, not an SZArrayHelper. See comments above //! or you may introduce a security hole! - throw new NotSupportedException(Environment.GetResourceString("NotSupported_ReadOnlyCollection")); + + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); } [SecuritySafeCritical] @@ -2765,17 +2785,18 @@ int IndexOf(T value) { void Insert(int index, T value) { // Not meaningful for arrays - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } bool Remove(T value) { // Not meaningful for arrays - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); + return default(bool); } void RemoveAt(int index) { // Not meaningful for arrays - throw new NotSupportedException(Environment.GetResourceString("NotSupported_FixedSizeCollection")); + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_FixedSizeCollection); } // This is a normal generic Enumerator for SZ arrays. It doesn't have any of the "this" stuff @@ -2807,8 +2828,8 @@ public bool MoveNext() { public T Current { get { - if (_index < 0) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted)); - if (_index >= _endIndex) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded)); + if (_index < 0) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted); + if (_index >= _endIndex) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded); return _array[_index]; } } diff --git a/src/mscorlib/src/System/Internal.cs b/src/mscorlib/src/System/Internal.cs index 8c9701d90391..c6be49f83f0e 100644 --- a/src/mscorlib/src/System/Internal.cs +++ b/src/mscorlib/src/System/Internal.cs @@ -47,6 +47,11 @@ static class Internal // This can be removed after V2, when we implement other schemes // of keeping the JIT-compiler out for generic instantiations. + // Method marked as NoOptimization as we don't want the JIT to + // inline any methods or take any short-circuit paths since the + // instantiation closure process is driven by "fixup" references + // left in the final code stream. + [MethodImplAttribute(MethodImplOptions.NoOptimization)] static void CommonlyUsedGenericInstantiations() { // Make absolutely sure we include some of the most common @@ -54,10 +59,11 @@ static void CommonlyUsedGenericInstantiations() // Note that reference type instantiations are already included // automatically for us. - System.Array.Sort(null); - System.Array.Sort(null); - System.Array.Sort(null); - + // Need to sort non null, len > 1 array or paths will short-circuit + Array.Sort(new double[1]); + Array.Sort(new int[1]); + Array.Sort(new IntPtr[1]); + new ArraySegment(new byte[1], 0, 0); new Dictionary(); diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index a021c9d74089..db1419d2cef1 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -47,6 +47,12 @@ internal static void ThrowArgumentOutOfRange_IndexException() { Environment.GetResourceString(GetResourceName(ExceptionResource.ArgumentOutOfRange_Index))); } + internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException() { + throw new ArgumentOutOfRangeException( + GetArgumentName(ExceptionArgument.index), + Environment.GetResourceString(GetResourceName(ExceptionResource.ArgumentOutOfRange_NeedNonNegNum))); + } + internal static void ThrowWrongKeyTypeArgumentException(object key, Type targetType) { throw new ArgumentException(Environment.GetResourceString("Arg_WrongType", key, targetType), "key"); } @@ -86,10 +92,19 @@ internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument Environment.GetResourceString(GetResourceName(resource))); } + internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource) { + throw new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", + Environment.GetResourceString(GetResourceName(resource))); + } + internal static void ThrowInvalidOperationException(ExceptionResource resource) { throw new InvalidOperationException(Environment.GetResourceString(GetResourceName(resource))); } + internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e) { + throw new InvalidOperationException(Environment.GetResourceString(GetResourceName(resource)), e); + } + internal static void ThrowSerializationException(ExceptionResource resource) { throw new SerializationException(Environment.GetResourceString(GetResourceName(resource))); } @@ -98,6 +113,10 @@ internal static void ThrowSecurityException(ExceptionResource resource) { throw new System.Security.SecurityException(Environment.GetResourceString(GetResourceName(resource))); } + internal static void ThrowRankException(ExceptionResource resource) { + throw new RankException(Environment.GetResourceString(GetResourceName(resource))); + } + internal static void ThrowNotSupportedException(ExceptionResource resource) { throw new NotSupportedException(Environment.GetResourceString(GetResourceName(resource))); } @@ -178,6 +197,27 @@ internal enum ExceptionArgument { action, comparison, offset, + newSize, + elementType, + length, + length1, + length2, + length3, + lengths, + len, + lowerBounds, + sourceArray, + destinationArray, + sourceIndex, + destinationIndex, + indices, + index1, + index2, + index3, + other, + comparer, + endIndex, + keys, } @@ -234,6 +274,21 @@ internal enum ExceptionResource { Argument_InvalidRegistryOptionsCheck, Argument_InvalidRegistryViewCheck, InvalidOperation_NullArray, + Arg_MustBeType, + Arg_NeedAtLeast1Rank, + ArgumentOutOfRange_HugeArrayNotSupported, + Arg_RanksAndBounds, + Arg_RankIndices, + Arg_Need1DArray, + Arg_Need2DArray, + Arg_Need3DArray, + NotSupported_FixedSizeCollection, + ArgumentException_OtherNotArrayOfCorrectLength, + Rank_MultiDimNotSupported, + InvalidOperation_IComparerFailed, + ArgumentOutOfRange_EndIndexStartIndex, + Arg_LowerBoundsMustMatch, + Arg_BogusIComparer, } }