.Net 中的元素是如何存储在容器中的?
.Net 中的元素是如何存储在容器中的? 例如,C++ 向量按顺序存储,而 List 则不然。
它们是如何针对 .Net 容器(Array、ArrayList...)实现的?
谢谢。
How are elements stored in containers in .Net?
For example, a C++ vector stores in sequential order, while List doesn't.
How are they implemented for .Net containers (Array, ArrayList, ...)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于元素。但是 C++
Vector
相当于 C#List
,C++List
相当于 C# LinkedList C#
ArrayList
几乎就是一个 C#List
百科列表 很多数据结构,我建议你看看那里,看看不同的数据结构是如何实现的。
所以:
It depends on the element. But a C++
Vector
is equivalent to a C#List
, and a C++List<T>
is equivalent to a C# LinkedListThe C#
ArrayList
is pretty much, a C#List<object>
Wikipedia lists many data structures, and I suggest you have a look there, to see how the different ones are implemented.
So:
它因容器而异。由于实现是专有的,因此没有公共规范来规定底层数据结构必须是什么。
如果您有兴趣花时间,我之前使用过以下工具来了解 MS 如何实现此类或那个:
It varies by container. Because the implementation is proprietary, there are no public specs mandating what the underlying data structure must be.
If you're interested in taking the time, I've used the following tools before to understand how MS implemented this class or that:
在 .net 中,容器(甚至数组)处理对它们的所有访问。您不使用指针来处理它们(除非在极少数情况下您可能永远不会接触到),因此它们如何存储信息通常并不重要。在许多情况下,甚至没有指定事情在幕后如何工作,因此可以将实现更改为“更好”的东西,而不会因为某些愚蠢的原因而破坏依赖于这些细节的东西。
不过,最后我听说,数组按顺序存储它们的条目——但需要注意的是,对于引用类型对象(所有不是结构的对象),“条目”是引用,而不是对象本身。数据可以位于内存中的任何位置。将其视为引用数组而不是对象数组。
ArrayLists 基于数组,应该以相同的方式存储它们的内容。
In .net, containers (even arrays) handle all access to them. You don't use pointers to work with them (except in extremely rare cases you probably won't ever get into), so it often doesn't matter how they store info. In many cases, it's not even specified how things work behind the scenes, so the implementation can be changed to something "better" without breaking stuff that relies on those details for some stupid reason.
Last i heard, though, arrays store their entries sequentially -- with the caveat that for reference-type objects (everything that's not a struct), "entries" are the references as opposed to the objects themselves. The data could be anywhere in memory. Think of it more like an array of references than an array of objects.
ArrayLists, being based on arrays, should store their stuff the same way.