0

I have the following:

 public static class LocalFileModelList
    {
        public static List<LocalFileModel> ModelList = new List<LocalFileModel>();
    }

    public class LocalFileModel
    {
        public string Name { get; set; }
        public string Extension { get; set; }
    }

Then a method to read all files from a directory.

public static List<LocalFileModel> GetAllFiles(string path)
{
    var files = Directory.GetFiles(path, "*.*");

    foreach(var file in files)
    {
        var Extension    = Path.GetExtension(file);
        var Filename     = Path.GetFileNameWithoutExtension(file);

        var model = new LocalFileModel
        {
            Name         = Filename,
            Extension    = Extension,
        };

        LocalFileModelList.ModelList.Add(model);     
    }

    return LocalFileModelList.ModelList;
}

I noticed that, as I step through my code, when I create a new instance of LocalFileModel, populate it with data then add it to the list. Automatically the list created three additional instances of type null. Once those three were populated with their respective objects, it would again create thre more null instances...

enter image description here

I just realized this now, this is normal?


Yes. .NET and most other libraries allocate a list or a vector with extra space (capacity) so it doesn't constantly have to resize and copy the data. The Size determines what is accessible.

The default capacity is defined in here to 4 (but the docs doesn't have to be):

http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,aa9d469618cd43b0,references

4

4 回答 4

5

List<T> has an internal array, with a certain capacity which is always equal to or greater than the number of items on the list.

list.Capacity >= list.Count

You can actually tell the list what capacity its internal array should be created with.

new List<int>(capacity: 5);

When an item is inserted, and the array is at its capacity, the list creates a new array with double the previous size to accommodate the new element. So, in your case, if you were to insert a 5th item, the list would allocate a new internal array with 8 slots (5 of which would be filled).

For more details, check the implementation here.

于 2015-05-25T15:56:45.390 回答
1

The initial capacity of the internal array held by List<T> is 4 (currently, that is an implementation detail and may change), granted you added an initial value. Once you start filling the list, it will resize itself by a multiple of 2 each time. That is why when you know ahead of time the number of minimum items, you can use the overload taking int capacity (or use an array if it's really a fixed size).

于 2015-05-25T15:58:17.990 回答
1

是的。.NET 和大多数其他库分配具有额外空间(容量)的列表或向量,因此它不必经常调整大小和复制数据。Size决定什么是可访问的。

默认容量在此处定义为 4(但文档不一定是):

http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,aa9d469618cd43b0,references

于 2015-05-25T15:56:10.777 回答
0

List<T> is backed by an array. The default/Initial capaicity of List<T> is defined as 4 as in reference source, but it doesn't take affect until an item is added to a list.

 public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
    {
        private const int _defaultCapacity = 4;

So when you added the first item in your List, the size is set to 4 using the following check in EnsureCapacity method:

int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;

Later on, with each new item being added to the list, the capacity increases to Number of Elements * 2

That is why, when you add the first item, you can see three null spaces, three additional spaces reserved in your list.

于 2015-05-25T16:07:17.320 回答