如何名单< T>内部映射? [英] How is List<T> internally mapped?

查看:167
本文介绍了如何名单< T>内部映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是否得到内部处理数组或是否得到视为一个完全不同类型的CLR?

Does it internally get treated as an Array or does it get treated as a totally different type by the CLR?

我想实现整数值到列表中。

I am trying to implement integer values to the list.

List<int> lst = new List<int>();
lst.Add(3);
lst.Add(4);

VS

我创建了一个整数数组

int[] arr = new int[2];
arr[0] = 3;
arr[1] = 4;

array返回更好的时间跨度结果。那么,为什么人们preFER名单&LT;>

Array returns better time span results. So why do people prefer List<>.

推荐答案

列表&LT;&GT; 是一个数据结构的实现,这需要在分配内存的护理在按需的基础;它允许插入和删除任意索引等,因此它是比简单的阵列方便得多。

List<> is an implementation of a data structure, which takes care of allocating memory on a on-demand basis; it allows for insertion and deletion at any index etc. Therefore it is much more convenient than a simple array.

引擎盖下,当前列表与LT;&GT; 实现使用存储阵列,并做类似数组的操作时的开销是最小的。更加方便,通常是值得的很少(如果在所有相关)的性能差异。添加项目通常是更快,因为列表分配的内存块,也不需要新的分配,并在每一个附加复制(相比于纯粹的数组,其中长度始终绑定到存储器的大小)

Under the hood, the current List<> implementation uses an array for storage, and the overhead when doing array-like operations is minimal. The added convenience is usually worth the little (if at all relevant) performance difference. Adding items is typically faster because the list allocates chunks of memory and doesn't require a new allocation and copy on every add (compared to the pure array, where the Length is always bound to the size in memory).

这篇关于如何名单&LT; T&GT;内部映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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