Does list.count physically enumerate through a list for counting or keep a pointer

I go through a large list of objects to do some things regarding the specified objects in the list.

During my iteration, I will remove some objects from the list depending on certain criteria.

Once everything is done, I need to update the user interface regarding the number of objects in my list. (List T).

Question:

When I call list.count, does .net actually iterate through the list, count it, or save the count as a property / variable?

If .net is physically repeating itself through the list, can I just save the counter on my own iteration in the list and save the overhead?

thanks

+6
source share
5 answers

It just stores an internal int to track the number of elements. So there are no iterations. The documentation says that getting 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

+15
source

List is implemented as a list of arrays and it keeps track of its own size, so calling the .Count property .Count not require any iteration.

If you call the LINQ .Count() extension method, it will check if the underlying IEnumerable<> ICollection (which is List<> ) implements, and if possible, use the .Count property on this interface. Thus, this will not result in any iteration.

By the way, there are other problems that you will encounter if you try to remove items from your list during iteration through it. It is not clear what the iteration should look like when you remove items from under the iterator, so List<> completely fix this problem by throwing an exception if the list has been modified since it was created.

+7
source

You can use a decompiler, such as the freely available ILSpy, to answer these questions. If you reference the type List<T> , then getter Count simply enables reading the field:

 public int Count { get { return this._size; } } 
+2
source

As stated here on the comments tab http://msdn.microsoft.com/en-us/library/27b47ht3 (v = vs .110) .aspx

Getting the value of this property is an O (1) operation.

This means that iteration does not occur.

+1
source

You noted your question with both vb.net and C #, so in response to "If .net is physically repeated through the list, can I just save the counter on my own iteration in the list and save overhead?"

If your iteration with For i = first To last , then VB.NET will evaluate first and last when it enters the loop:

 Dim first As Integer = 1 Dim last As Integer = 3 For i = first To last Console.Write(i.ToString() & " ") last = -99 Next 

: 1 2 3

If you are performing an equivalent in C #, first and last are evaluated at each iteration:

 int first = 1; int last = 1; for (int i = first; i <= last; i++) { Console.Write(i.ToString() + " "); last = -99; } 

outputs: 1

If your .Count () / property function is expensively priced and / or you do not want it to be reevaluated at each iteration (for some other reason), then in C # you can assign it to a temporary variable.

+1
source

Source: https://habr.com/ru/post/971612/


All Articles