调整 List<T> 的容量 [英] Resize capacity of List&lt;T&gt;

查看:22
本文介绍了调整 List<T> 的容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用反射器阅读了.NET4.0的System.Collections.Generic.List类的源代码,我有一些问题.代码如下:

I read the source code of the class System.Collections.Generic.List<T> of .NET4.0 with Reflector, and I have some questions. Here is the code:

    [__DynamicallyInvokable]
    public void Add(T item)
    {
        if (this._size == this._items.Length)
        {
            this.EnsureCapacity(this._size + 1);
        }
        this._items[this._size++] = item;
        this._version++;
    }
    private void EnsureCapacity(int min)
    {
        if (this._items.Length < min)
        {
            int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
            if (num > 0x7fefffff)
            {
                num = 0x7fefffff;
            }
            if (num < min)
            {
                num = min;
            }
            this.Capacity = num;
        }
    }
    [__DynamicallyInvokable]
    public int Capacity
    {
        // omitted code

        set
        {
            if (value < this._size)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
            }
            if (value != this._items.Length)
            {
                if (value > 0)
                {
                    T[] destinationArray = new T[value];
                    if (this._size > 0)
                    {
                        Array.Copy(this._items, 0, destinationArray, 0, this._size);
                    }
                    this._items = destinationArray;
                }
                else
                {
                    this._items = List<T>._emptyArray;
                }
            }
        }
    }

  • 通过将所有元素复制到新数组来调整数组大小是最好的方法还是唯一的方法?

    • Is it the best way or the only way to resize the array by copying all elements to a new array?

      为什么要检查num"?与0x7fefffff"?为什么是0x7fefffff"很特别吗?

      Why check the "num" with "0x7fefffff"? Why "0x7fefffff" is special?

      为什么他们可以使用4"?和0x7fefffff"直接地?它们不是神奇的数字吗?

      Why they can use "4" and "0x7fefffff" directly? Aren't they magic numbers?

      谢谢.

      推荐答案

      通过将所有元素复制到新数组来调整数组大小是最好的方法还是唯一的方法?

      Is it the best way or the only way to resize the array by copying all elements to a new array?

      是的,这是调整数组大小的唯一方法.

      Yes, that's the only way to resize an array.

      对于给定大小的数组,只为该数组分配那部分内存,周围的内存可以分配给其他东西,所以不能简单地扩展内存,需要更大的槽.

      For a given size array, only that section of memory is allocated for the array, the surrounding memory could be allocated to other things, so you can't simply expand the memory, you need a bigger slot.

       

      为什么用0x7ffeffff"检查num"?为什么0x7fefffff"很特别?

      Why check the "num" with "0x7fefffff"? Why "0x7fefffff" is special?

      请参阅 MSDN:

      但是,数组仍将限制为总共 40 亿个元素,并且任何给定维度中的最大索引为 0X7FEFFFFF(0X7FFFFFC7 用于字节数组和单字节结构数组).

      However, the array will still be limited to a total of 4 billion elements, and to a maximum index of 0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and arrays of single-byte structures).

      我找不到任何文档来说明为什么 0X7FEFFFFF 是允许的最大大小(他们可能有一些很好的理由来设置这样的限制).

      I couldn't find any documentation to indicate why 0X7FEFFFFF is the maximum allowed size though (they probably have some decent good reasons for such a limit).

       

      为什么他们可以直接使用4"和0x7ffeffff"?它们不是神奇的数字吗?

      Why they can use "4" and "0x7fefffff" directly? Aren't they magic numbers?

      const 在这里可能有意义.

      这可能不是完全原始代码的样子(正如 usr 提到的,它可以被反编译),所以确实可以使用 const.

      This may not be exactly what the original code looks like (as usr mentions, it could've been decompiled), so there indeed could've been const's used.

      如果恰好是原始代码的样子,我敢肯定只有开发人员才能告诉您他们为什么要这样做.

      If it happens to be what the original code looks like, I'm sure only the developers can tell you why they did what they did.

      这篇关于调整 List<T> 的容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆