T IReadOnlyList <T> .this[int index] { get { // Following trick can reduce the range check by one if ((uint)index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } Contract.EndContractBlock(); return(_items[index]); } }
// Sets or Gets the element at the given index. // public ref T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if ((uint)index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } Contract.EndContractBlock(); return(ref _items[index]); } }
// Returns the index of the first occurrence of a given value in a range of // this list. The list is searched forwards, starting at index // index and upto count number of elements. The // elements of the list are compared to the given value using the // Object.Equals method. // // This method uses the Array.IndexOf method to perform the // search. // public int IndexOf(T item, int index, int count) { if (index > _size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } if (count < 0 || index > _size - count) { ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); } Contract.Ensures(Contract.Result <int>() >= -1); Contract.Ensures(Contract.Result <int>() < Count); Contract.EndContractBlock(); return(Array.IndexOf(_items, item, index, count)); }
// Removes the element at the given index. The size of the list is // decreased by one. // public void RemoveAt(int index) { if ((uint)index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } Contract.EndContractBlock(); _size--; if (index < _size) { Array.Copy(_items, index + 1, _items, index, _size - index); } if (TypeHelper <T> .Size <= 0) { _items[_size] = default(T); } _version++; }
// Sets or Gets the element at the given index. // public ref T this[int index] { get { // Following trick can reduce the range check by one if ((uint)index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } Contract.EndContractBlock(); return(ref _items[index]); } //set //{ // if ((uint)index >= (uint)_size) // { // ThrowHelper.ThrowArgumentOutOfRange_IndexException(); // } // Contract.EndContractBlock(); // _items[index] = value; // _version++; //} }
// Inserts the elements of the given collection at a given index. If // required, the capacity of the list is increased to twice the previous // capacity or the new size, whichever is larger. Ranges may be added // to the end of the list by setting index to the List's size. // public void InsertRange(int index, IEnumerable <T> collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } if ((uint)index > (uint)_size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } Contract.EndContractBlock(); ICollection <T> c = collection as ICollection <T>; if (c != null) { // if collection is ICollection<T> int count = c.Count; if (count > 0) { EnsureCapacity(_size + count); if (index < _size) { Array.Copy(_items, index, _items, index + count, _size - index); } // If we're inserting a List into itself, we want to be able to deal with that. // ReSharper disable once PossibleUnintendedReferenceComparison if (this == c) { // Copy first part of _items to insert location Array.Copy(_items, 0, _items, index, index); // Copy last part of _items back to inserted location Array.Copy(_items, index + count, _items, index * 2, _size - index); } else { c.CopyTo(_items, index); } _size += count; } } else if (index < _size) { // We're inserting a lazy enumerable. Call Insert on each of the constituent items. // ReSharper disable once PossibleNullReferenceException using (IEnumerator <T> en = collection.GetEnumerator()) { while (en.MoveNext()) { Insert(index++, en.Current); } } } else { // We're adding a lazy enumerable because the index is at the end of this list. AddEnumerable(collection); } _version++; }