/// <summary> /// 获取或设置指定索引处的元素。 /// </summary> /// <param name="index">要获得或设置的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> /// <exception cref="ArgumentException">设置属性,且 <paramref name="value"/> 不能赋值给 /// <typeparamref name="T"/> 类型。</exception> object IList.this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); return(this.GetItemAt(index)); } set { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); try { this.SetItemAt(index, (T)value); } catch (InvalidCastException) { throw CommonExceptions.ArgumentWrongType("value", value, typeof(T)); } } }
/// <summary> /// 将指定集合的元素插入到当前列表的指定索引处。 /// </summary> /// <typeparam name="T">列表中元素的类型。</typeparam> /// <param name="list">要插入到的列表。</param> /// <param name="index">从零开始的索引,在此处开始插入新元素。</param> /// <param name="collection">要插入的元素集合。</param> /// <exception cref="ArgumentNullException"><paramref name="list"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="collection"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <paramref name="list"/> 中的有效索引。</exception> public static void InsertRange <T>(this IList <T> list, int index, IEnumerable <T> collection) { if (list == null) { throw CommonExceptions.ArgumentNull("list"); } if (collection == null) { throw CommonExceptions.ArgumentNull("collection"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index >= list.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); foreach (T item in collection) { list.Insert(index++, item); } }
/// <summary> /// 从 <see cref="BitList"/> 中移除一定范围的元素。 /// </summary> /// <param name="index">要移除的元素的范围从零开始的起始索引。</param> /// <param name="length">要移除的元素数。</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> 和 <paramref name="length"/> 不表示 /// <see cref="BitList"/> 中元素的有效范围。</exception> public void RemoveRange(int index, int length) { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (length < 0) { throw CommonExceptions.ArgumentNegative("length", length); } if (index + length > this.count) { throw CommonExceptions.ArgumentOutOfRange("length", length); } Contract.EndContractBlock(); if (length <= 0) { return; } if (length == 1) { this.RemoveItem(index); return; } int valueIdx = (index + length) >> IndexShift; int idx = index >> IndexShift; int tailSize = index & IndexMask; int rSize = (index + length) & IndexMask; if (rSize > 0) { uint value = this.items[valueIdx]; int highSize = tailSize == 0 ? 0 : UnitSize - tailSize; int restSize = UnitSize - rSize; if (highSize > restSize) { highSize = restSize; } if (highSize > 0) { uint tailMask = GetMask(tailSize); this.items[idx] = (this.items[idx] & tailMask) | ((value >> rSize) << tailSize); tailSize += highSize; if (tailSize == UnitSize) { idx++; tailSize = 0; } } if (restSize > highSize) { tailSize = restSize - highSize; items[idx] = value >> (UnitSize - tailSize); } valueIdx++; } // 计算要复制的长度。 int len = this.count - (valueIdx << IndexShift) + tailSize; if (len > 0) { this.CopyItems(this.items, valueIdx, idx, tailSize, len); } this.count -= length; }