/// <summary> /// 从特定的 <see cref="Array"/> 索引处开始,将指定集合 /// 的元素复制到一个 <see cref="Array"/> 中。 /// </summary> /// <param name="source">要复制元素的集合。</param> /// <param name="array">从 <paramref name="source"/> 复制的元素的目标位置的一维 /// <see cref="Array"/>。<paramref name="array"/> 必须具有从零开始的索引。</param> /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="array"/> 是多维的。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 中的元素数目大于从 <paramref name="index"/> 到目标 <paramref name="array"/> /// 末尾之间的可用空间。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception> public static void CopyTo <T>(ICollection <T> source, Array array, int index) { Contract.Requires(source != null); CommonExceptions.CheckSimplyArray(array, nameof(array)); if (index < 0) { throw CommonExceptions.ArgumentNegative(nameof(index), index); } if (array.Length - index < source.Count) { throw CommonExceptions.ArrayTooSmall(nameof(array)); } Contract.EndContractBlock(); var arr = array as T[]; if (arr != null) { foreach (var obj in source) { arr[index++] = obj; } } else { try { foreach (var obj in source) { array.SetValue(obj, index++); } } catch (InvalidCastException ex) { throw CommonExceptions.InvalidElementType(ex); } } }
/// <summary> /// 从特定的 <see cref="System.Array"/> 索引处开始,将 /// <see cref="CollectionBase{T}"/> 的元素复制到一个 <see cref="System.Array"/> 中。 /// </summary> /// <param name="array">作为从 <see cref="CollectionBase{T}"/> /// 复制的元素的目标位置的一维 <see cref="System.Array"/>。 /// <see cref="System.Array"/> 必须具有从零开始的索引。</param> /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param> /// <exception cref="ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> 小于零。</exception> /// <exception cref="System.ArgumentException"> /// <paramref name="array"/> 是多维的。</exception> /// <exception cref="System.ArgumentException">源 <see cref="CollectionBase{T}"/> /// 中的元素数目大于从 <paramref name="index"/> 到目标 /// <paramref name="array"/> 末尾之间的可用空间。</exception> /// <exception cref="System.ArgumentException">源 <see cref="CollectionBase{T}"/> /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception> void ICollection.CopyTo(Array array, int index) { CommonExceptions.CheckSimplyArray(array, "array"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } Contract.EndContractBlock(); uint[] uarr = array as uint[]; if (uarr != null) { int end = 0; if (this.count > 0) { end = (this.count - 1) >> IndexShift; } Array.Copy(this.items, 0, array, index, end + 1); return; } int[] iarr = array as int[]; if (iarr != null) { int cnt = 1; if (this.count > 0) { cnt = ((this.count - 1) >> IndexShift) + 1; } if (array.Length - index < cnt) { throw CommonExceptions.ArrayTooSmall("array"); } for (int i = 0; i < cnt; i++) { iarr[index++] = unchecked ((int)this.items[i]); } return; } byte[] barr = array as byte[]; if (barr != null) { int cnt = 1; if (this.count > 0) { cnt = ((this.count - 1) / 8) + 1; } if (array.Length - index < cnt) { throw CommonExceptions.ArrayTooSmall("array"); } for (int i = 0; i < cnt; i++) { barr[index++] = (byte)((this.items[i / 4] >> ((i % 4) * 8)) & 0xFF); } return; } bool[] boarr = array as bool[]; if (boarr != null) { if (array.Length - index < this.count) { throw CommonExceptions.ArrayTooSmall("array"); } for (int i = 0; i < this.count; i++) { boarr[index++] = (this.items[i >> IndexShift] & (1U << (i & IndexMask))) > 0; } return; } throw CommonExceptions.InvalidElementType(); }
/// <summary> /// 从特定的 <see cref="System.Array"/> 索引处开始,将 /// <see cref="CollectionBase{T}"/> 的元素复制到一个 <see cref="System.Array"/> 中。 /// </summary> /// <param name="array">作为从 <see cref="CollectionBase{T}"/> /// 复制的元素的目标位置的一维 <see cref="System.Array"/>。 /// <see cref="System.Array"/> 必须具有从零开始的索引。</param> /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param> /// <exception cref="ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> 小于零。</exception> /// <exception cref="System.ArgumentException"> /// <paramref name="array"/> 是多维的。</exception> /// <exception cref="System.ArgumentException">源 <see cref="CollectionBase{T}"/> /// 中的元素数目大于从 <paramref name="index"/> 到目标 /// <paramref name="array"/> 末尾之间的可用空间。</exception> /// <exception cref="System.ArgumentException">源 <see cref="CollectionBase{T}"/> /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception> void ICollection.CopyTo(Array array, int index) { CommonExceptions.CheckSimplyArray(array, nameof(array)); CommonExceptions.CheckArgumentNegative(nameof(index), index); Contract.EndContractBlock(); var uarr = array as uint[]; if (uarr != null) { var end = 0; if (_count > 0) { end = (_count - 1) >> IndexShift; } Array.Copy(_items, 0, array, index, end + 1); return; } var iarr = array as int[]; if (iarr != null) { var cnt = 1; if (_count > 0) { cnt = ((_count - 1) >> IndexShift) + 1; } if (array.Length - index < cnt) { throw CommonExceptions.ArrayTooSmall(nameof(array)); } for (var i = 0; i < cnt; i++) { iarr[index++] = unchecked ((int)_items[i]); } return; } var barr = array as byte[]; if (barr != null) { var cnt = 1; if (_count > 0) { cnt = (_count - 1) / 8 + 1; } if (array.Length - index < cnt) { throw CommonExceptions.ArrayTooSmall(nameof(array)); } for (var i = 0; i < cnt; i++) { barr[index++] = (byte)((_items[i / 4] >> (i % 4 * 8)) & 0xFF); } return; } var boarr = array as bool[]; if (boarr != null) { if (array.Length - index < _count) { throw CommonExceptions.ArrayTooSmall(nameof(array)); } for (var i = 0; i < _count; i++) { boarr[index++] = (_items[i >> IndexShift] & (1U << (i & IndexMask))) > 0; } return; } throw CommonExceptions.InvalidElementType(); }