Does list.count physically iterate through the list to count it, or does it keep a pointer

后端 未结 5 1940
臣服心动
臣服心动 2021-01-11 12:55

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 depen

5条回答
  •  情歌与酒
    2021-01-11 13:56

    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:

    http://msdn.microsoft.com/en-us/library/27b47ht3%28v=vs.110%29.aspx

    You can see for yourself:

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

提交回复
热议问题