相关疑难解决方法(0)

List <T> .AddRange实现次优

分析我的C#应用​​程序表明花费了大量时间List<T>.AddRange.使用Reflector查看此方法中的代码表明它调用的List<T>.InsertRange是这样实现的:

public void InsertRange(int index, IEnumerable<T> collection)
{
    if (collection == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
    }
    if (index > this._size)
    {
        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
    }
    ICollection<T> is2 = collection as ICollection<T>;
    if (is2 != null)
    {
        int count = is2.Count;
        if (count > 0)
        {
            this.EnsureCapacity(this._size + count);
            if (index < this._size)
            {
                Array.Copy(this._items, index, this._items, index + count, this._size - index);
            }
            if (this == is2)
            {
                Array.Copy(this._items, 0, this._items, index, index);
                Array.Copy(this._items, (int) (index + count), …
Run Code Online (Sandbox Code Playgroud)

.net c# performance list addrange

20
推荐指数
1
解决办法
9242
查看次数

标签 统计

.net ×1

addrange ×1

c# ×1

list ×1

performance ×1