mscorlib(4.0.0.0) API with additions
ArraySegment.cs
1 using System.Collections;
3 
4 namespace System
5 {
9  [__DynamicallyInvokable]
11  {
12  [Serializable]
13  private sealed class ArraySegmentEnumerator : IEnumerator<T>, IDisposable, IEnumerator
14  {
15  private T[] _array;
16 
17  private int _start;
18 
19  private int _end;
20 
21  private int _current;
22 
23  public T Current
24  {
25  get
26  {
27  if (_current < _start)
28  {
29  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
30  }
31  if (_current >= _end)
32  {
33  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
34  }
35  return _array[_current];
36  }
37  }
38 
39  object IEnumerator.Current
40  {
41  get
42  {
43  return Current;
44  }
45  }
46 
47  internal ArraySegmentEnumerator(ArraySegment<T> arraySegment)
48  {
49  _array = arraySegment._array;
50  _start = arraySegment._offset;
51  _end = _start + arraySegment._count;
52  _current = _start - 1;
53  }
54 
55  public bool MoveNext()
56  {
57  if (_current < _end)
58  {
59  _current++;
60  return _current < _end;
61  }
62  return false;
63  }
64 
65  void IEnumerator.Reset()
66  {
67  _current = _start - 1;
68  }
69 
70  public void Dispose()
71  {
72  }
73  }
74 
75  private T[] _array;
76 
77  private int _offset;
78 
79  private int _count;
80 
83  [__DynamicallyInvokable]
84  public T[] Array
85  {
86  [__DynamicallyInvokable]
87  get
88  {
89  return _array;
90  }
91  }
92 
95  [__DynamicallyInvokable]
96  public int Offset
97  {
98  [__DynamicallyInvokable]
99  get
100  {
101  return _offset;
102  }
103  }
104 
107  [__DynamicallyInvokable]
108  public int Count
109  {
110  [__DynamicallyInvokable]
111  get
112  {
113  return _count;
114  }
115  }
116 
123  [__DynamicallyInvokable]
124  T IList<T>.this[int index]
125  {
126  [__DynamicallyInvokable]
127  get
128  {
129  if (_array == null)
130  {
131  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
132  }
133  if (index < 0 || index >= _count)
134  {
135  throw new ArgumentOutOfRangeException("index");
136  }
137  return _array[_offset + index];
138  }
139  [__DynamicallyInvokable]
140  set
141  {
142  if (_array == null)
143  {
144  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
145  }
146  if (index < 0 || index >= _count)
147  {
148  throw new ArgumentOutOfRangeException("index");
149  }
150  _array[_offset + index] = value;
151  }
152  }
153 
160  [__DynamicallyInvokable]
161  T IReadOnlyList<T>.this[int index]
162  {
163  [__DynamicallyInvokable]
164  get
165  {
166  if (_array == null)
167  {
168  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
169  }
170  if (index < 0 || index >= _count)
171  {
172  throw new ArgumentOutOfRangeException("index");
173  }
174  return _array[_offset + index];
175  }
176  }
177 
181  [__DynamicallyInvokable]
182  bool ICollection<T>.IsReadOnly
183  {
184  [__DynamicallyInvokable]
185  get
186  {
187  return true;
188  }
189  }
190 
195  [__DynamicallyInvokable]
196  public ArraySegment(T[] array)
197  {
198  if (array == null)
199  {
200  throw new ArgumentNullException("array");
201  }
202  _array = array;
203  _offset = 0;
204  _count = array.Length;
205  }
206 
217  [__DynamicallyInvokable]
218  public ArraySegment(T[] array, int offset, int count)
219  {
220  if (array == null)
221  {
222  throw new ArgumentNullException("array");
223  }
224  if (offset < 0)
225  {
226  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
227  }
228  if (count < 0)
229  {
230  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
231  }
232  if (array.Length - offset < count)
233  {
234  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
235  }
236  _array = array;
237  _offset = offset;
238  _count = count;
239  }
240 
243  [__DynamicallyInvokable]
244  public override int GetHashCode()
245  {
246  if (_array != null)
247  {
248  return _array.GetHashCode() ^ _offset ^ _count;
249  }
250  return 0;
251  }
252 
257  [__DynamicallyInvokable]
258  public override bool Equals(object obj)
259  {
260  if (obj is ArraySegment<T>)
261  {
262  return Equals((ArraySegment<T>)obj);
263  }
264  return false;
265  }
266 
271  [__DynamicallyInvokable]
272  public bool Equals(ArraySegment<T> obj)
273  {
274  if (obj._array == _array && obj._offset == _offset)
275  {
276  return obj._count == _count;
277  }
278  return false;
279  }
280 
286  [__DynamicallyInvokable]
287  public static bool operator ==(ArraySegment<T> a, ArraySegment<T> b)
288  {
289  return a.Equals(b);
290  }
291 
297  [__DynamicallyInvokable]
298  public static bool operator !=(ArraySegment<T> a, ArraySegment<T> b)
299  {
300  return !(a == b);
301  }
302 
306  [__DynamicallyInvokable]
307  int IList<T>.IndexOf(T item)
308  {
309  if (_array == null)
310  {
311  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
312  }
313  int num = System.Array.IndexOf(_array, item, _offset, _count);
314  if (num < 0)
315  {
316  return -1;
317  }
318  return num - _offset;
319  }
320 
327  [__DynamicallyInvokable]
328  void IList<T>.Insert(int index, T item)
329  {
330  throw new NotSupportedException();
331  }
332 
338  [__DynamicallyInvokable]
339  void IList<T>.RemoveAt(int index)
340  {
341  throw new NotSupportedException();
342  }
343 
347  [__DynamicallyInvokable]
348  void ICollection<T>.Add(T item)
349  {
350  throw new NotSupportedException();
351  }
352 
355  [__DynamicallyInvokable]
356  void ICollection<T>.Clear()
357  {
358  throw new NotSupportedException();
359  }
360 
365  [__DynamicallyInvokable]
366  bool ICollection<T>.Contains(T item)
367  {
368  if (_array == null)
369  {
370  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
371  }
372  int num = System.Array.IndexOf(_array, item, _offset, _count);
373  return num >= 0;
374  }
375 
385  [__DynamicallyInvokable]
386  void ICollection<T>.CopyTo(T[] array, int arrayIndex)
387  {
388  if (_array == null)
389  {
390  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
391  }
392  System.Array.Copy(_array, _offset, array, arrayIndex, _count);
393  }
394 
400  [__DynamicallyInvokable]
401  bool ICollection<T>.Remove(T item)
402  {
403  throw new NotSupportedException();
404  }
405 
408  [__DynamicallyInvokable]
410  {
411  if (_array == null)
412  {
413  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
414  }
415  return new ArraySegmentEnumerator(this);
416  }
417 
420  [__DynamicallyInvokable]
422  {
423  if (_array == null)
424  {
425  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
426  }
427  return new ArraySegmentEnumerator(this);
428  }
429  }
430 }
bool Equals(ArraySegment< T > obj)
Determines whether the specified T:System.ArraySegment`1 structure is equal to the current instance.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
static bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Indicates whether two T:System.ArraySegment`1 structures are equal.
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
void RemoveAt(int index)
Removes the T:System.Collections.IList item at the specified index.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
void Insert(int index, object value)
Inserts an item to the T:System.Collections.IList at the specified index.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
int Offset
Gets the position of the first element in the range delimited by the array segment,...
Definition: ArraySegment.cs:97
Represents a strongly-typed, read-only collection of elements.
int IndexOf(object value)
Determines the index of a specific item in the T:System.Collections.IList.
Represents a read-only collection of elements that can be accessed by index.
Definition: IReadOnlyList.cs:9
override int GetHashCode()
Returns the hash code for the current instance.
override bool Equals(object obj)
Determines whether the specified object is equal to the current instance.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
ArraySegment(T[] array, int offset, int count)
Initializes a new instance of the T:System.ArraySegment`1 structure that delimits the specified range...
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
ArraySegment(T[] array)
Initializes a new instance of the T:System.ArraySegment`1 structure that delimits all the elements in...
The exception that is thrown when one of the arguments provided to a method is not valid.
int Count
Gets the number of elements in the range delimited by the array segment.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
static bool operator !=(ArraySegment< T > a, ArraySegment< T > b)
Indicates whether two T:System.ArraySegment`1 structures are unequal.
Specifies that the class can be serialized.
The exception that is thrown when a method call is invalid for the object's current state.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Delimits a section of a one-dimensional array.
Definition: ArraySegment.cs:10