难道list.count物理通过列表迭代来算呢,还是保持它的指针 [英] Does list.count physically iterate through the list to count it, or does it keep a pointer
问题描述
我踩着通过对象的大名单做一些东西就表示,在列表中的对象。
I am stepping through a large list of object to do some stuff regarding said objects in the list.
在我的反复,我将从列表中删除某些对象根据一定的标准。
During my iteration, I will remove some objects from the list depending on certain criteria.
在全部完成后,我需要更新关于我的列表中的对象数量的用户界面。 (T列表)。
Once all is done, I need to update the UI regarding the number of objects in my list. (List of T).
问:
当我打电话list.count,并实际.NET遍历列表 算呢,还是它的计数存储为一个属性/变量?
When I call list.count, does .net actually iterate through the list to count it, or does it store the count as a property/variable?
如果.NET身体重新遍历列表,我可能只是以及继续我自己的迭代通过列表中的一个计数器,并保存开销?
If .net physically re-iterates through the list, I may just as well keep a counter on my own iteration through the list, and save the overhead?
感谢
推荐答案
它只是保持一个内部INT跟踪的项目数。因此,没有重复。 该文件说,检索次数是O(1)操作:
It simply keeps an internal int to track the number of items. So no iteration. The documentation says retrieving Count is an O(1) operation:
<一个href="http://msdn.microsoft.com/en-us/library/27b47ht3%28v=vs.110%29.aspx">http://msdn.microsoft.com/en-us/library/27b47ht3%28v=vs.110%29.aspx
您自己可以看到:
<一个href="http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs">http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs
这篇关于难道list.count物理通过列表迭代来算呢,还是保持它的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!