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

Commit a90f60e

Browse files
benaadamsjkotas
authored andcommitted
ArraySegment<T> exceptions to ThrowHelper (#7058)
1 parent fc4444c commit a90f60e

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/mscorlib/src/System/ArraySegment.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
3535
public ArraySegment(T[] array)
3636
{
3737
if (array == null)
38-
throw new ArgumentNullException("array");
38+
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
3939
Contract.EndContractBlock();
4040

4141
_array = array;
@@ -46,13 +46,13 @@ public ArraySegment(T[] array)
4646
public ArraySegment(T[] array, int offset, int count)
4747
{
4848
if (array == null)
49-
throw new ArgumentNullException("array");
49+
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
5050
if (offset < 0)
51-
throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
51+
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
5252
if (count < 0)
53-
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
53+
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
5454
if (array.Length - offset < count)
55-
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
55+
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
5656
Contract.EndContractBlock();
5757

5858
_array = array;
@@ -146,9 +146,9 @@ T IList<T>.this[int index]
146146
get
147147
{
148148
if (_array == null)
149-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
149+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
150150
if (index < 0 || index >= _count)
151-
throw new ArgumentOutOfRangeException("index");
151+
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
152152
Contract.EndContractBlock();
153153

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

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

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

183183
void IList<T>.Insert(int index, T item)
184184
{
185-
throw new NotSupportedException();
185+
ThrowHelper.ThrowNotSupportedException();
186186
}
187187

188188
void IList<T>.RemoveAt(int index)
189189
{
190-
throw new NotSupportedException();
190+
ThrowHelper.ThrowNotSupportedException();
191191
}
192192
#endregion
193193

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

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

221221
void ICollection<T>.Add(T item)
222222
{
223-
throw new NotSupportedException();
223+
ThrowHelper.ThrowNotSupportedException();
224224
}
225225

226226
void ICollection<T>.Clear()
227227
{
228-
throw new NotSupportedException();
228+
ThrowHelper.ThrowNotSupportedException();
229229
}
230230

231231
bool ICollection<T>.Contains(T item)
232232
{
233233
if (_array == null)
234-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
234+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
235235
Contract.EndContractBlock();
236236

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

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

254254
bool ICollection<T>.Remove(T item)
255255
{
256-
throw new NotSupportedException();
256+
ThrowHelper.ThrowNotSupportedException();
257+
return default(bool);
257258
}
258259
#endregion
259260

260261
#region IEnumerable<T>
261262
IEnumerator<T> IEnumerable<T>.GetEnumerator()
262263
{
263264
if (_array == null)
264-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
265+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
265266
Contract.EndContractBlock();
266267

267268
return new ArraySegmentEnumerator(this);
@@ -272,7 +273,7 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
272273
IEnumerator IEnumerable.GetEnumerator()
273274
{
274275
if (_array == null)
275-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
276+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
276277
Contract.EndContractBlock();
277278

278279
return new ArraySegmentEnumerator(this);
@@ -314,8 +315,8 @@ public T Current
314315
{
315316
get
316317
{
317-
if (_current < _start) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
318-
if (_current >= _end) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
318+
if (_current < _start) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted);
319+
if (_current >= _end) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded);
319320
return _array[_current];
320321
}
321322
}

src/mscorlib/src/System/ThrowHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ internal static void ThrowObjectDisposedException(string objectName, ExceptionRe
110110
throw new ObjectDisposedException(objectName, Environment.GetResourceString(GetResourceName(resource)));
111111
}
112112

113+
internal static void ThrowNotSupportedException()
114+
{
115+
throw new NotSupportedException();
116+
}
117+
113118
// Allow nulls for reference types and Nullable<U>, but not for value types.
114119
// Aggressively inline so the jit evaluates the if in place and either drops the call altogether
115120
// Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
@@ -171,7 +176,9 @@ internal enum ExceptionArgument {
171176
view,
172177
sourceBytesToCopy,
173178
action,
174-
comparison
179+
comparison,
180+
offset,
181+
175182
}
176183

177184
//
@@ -225,7 +232,9 @@ internal enum ExceptionResource {
225232
ObjectDisposed_RegKeyClosed,
226233
NotSupported_InComparableType,
227234
Argument_InvalidRegistryOptionsCheck,
228-
Argument_InvalidRegistryViewCheck
235+
Argument_InvalidRegistryViewCheck,
236+
InvalidOperation_NullArray,
237+
229238
}
230239
}
231240

0 commit comments

Comments
 (0)