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

Commit 385b3b6

Browse files
committed
ArraySegment exceptions to ThrowHelper
1 parent aa7511f commit 385b3b6

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/mscorlib/src/System/ArraySegment.cs

Lines changed: 25 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.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
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.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
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.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
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,25 @@ 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);
258+
257259
}
258260
#endregion
259261

260262
#region IEnumerable<T>
261263
IEnumerator<T> IEnumerable<T>.GetEnumerator()
262264
{
263265
if (_array == null)
264-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
266+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
265267
Contract.EndContractBlock();
266268

267269
return new ArraySegmentEnumerator(this);
@@ -272,7 +274,7 @@ IEnumerator<T> IEnumerable<T>.GetEnumerator()
272274
IEnumerator IEnumerable.GetEnumerator()
273275
{
274276
if (_array == null)
275-
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
277+
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
276278
Contract.EndContractBlock();
277279

278280
return new ArraySegmentEnumerator(this);
@@ -314,8 +316,8 @@ public T Current
314316
{
315317
get
316318
{
317-
if (_current < _start) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
318-
if (_current >= _end) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
319+
if (_current < _start) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumNotStarted);
320+
if (_current >= _end) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumEnded);
319321
return _array[_current];
320322
}
321323
}

src/mscorlib/src/System/ThrowHelper.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ internal static void ThrowSecurityException(ExceptionResource resource) {
102102
throw new System.Security.SecurityException(Environment.GetResourceString(GetResourceName(resource)));
103103
}
104104

105+
internal static void ThrowNotSupportedException()
106+
{
107+
throw new NotSupportedException();
108+
}
109+
105110
internal static void ThrowNotSupportedException(ExceptionResource resource) {
106111
throw new NotSupportedException(Environment.GetResourceString(GetResourceName(resource)));
107112
}
@@ -292,6 +297,10 @@ internal static string GetArgumentName(ExceptionArgument argument) {
292297
argumentName = "indices";
293298
break;
294299

300+
case ExceptionArgument.offset:
301+
argumentName = "offset";
302+
break;
303+
295304
default:
296305
Contract.Assert(false, "The enum value is not defined, please checked ExceptionArgumentName Enum.");
297306
return string.Empty;
@@ -509,6 +518,10 @@ internal static string GetResourceName(ExceptionResource resource) {
509518
resourceName = "Arg_MustBeType";
510519
break;
511520

521+
case ExceptionResource.InvalidOperation_NullArray:
522+
resourceName = "InvalidOperation_NullArray";
523+
break;
524+
512525
default:
513526
Contract.Assert( false, "The enum value is not defined, please checked ExceptionArgumentName Enum.");
514527
return string.Empty;
@@ -563,7 +576,8 @@ internal enum ExceptionArgument {
563576
index1,
564577
index2,
565578
index3,
566-
indices
579+
indices,
580+
offset
567581
}
568582

569583
//
@@ -622,7 +636,8 @@ internal enum ExceptionResource {
622636
InvalidOperation_IComparerFailed,
623637
ArgumentOutOfRange_HugeArrayNotSupported,
624638
NotSupported_FixedSizeCollection,
625-
Arg_MustBeType
639+
Arg_MustBeType,
640+
InvalidOperation_NullArray
626641
}
627642
}
628643

0 commit comments

Comments
 (0)