Does list<>.Count, itterate over the array or is it a stored value?

This may be a stupid question but when you call list.Count or array.Length,
Are those stored values that are incremented internally for reference with a new item is added to the index or does it iterate over the array when it is called to get a count on request?

I am just wondering if it is actually necessary for me to store and reference my own counts or not for optimization purposes. Obviously that would not be necessary if the count is already a stored value and the array is not iterated over when the count is requested.

Tried looking at the Count in VS but all I can see is the interface for it, not the actual code. I would like to see the actual code running the List class.

It’s stored. It’s O(1) time reading it i.e. constant time.

All list implementations are like this because they need to keep both a capacity and an actual content size. Arrays are fixed in memory so they store the fixed content size.

You could test this with the profiler. Create a list with one million ints in it and one with a single int and access both.

1 Like

Here’s the reference source code: Reference Source

1 Like

Your question is very complicated. You want to know if Count actively counts the elements in the list.
The answer is no, that would be silly. It is safely stored inside the list as a counter, and every time you add or remove, this counter is modified to reflect the state.

What you asked btw, is ground-level knowledge in general programming. And it’s ok to not know it, you ask, you get an answer. Nothing wrong with that.

I just wanted to say that if you’re at a level where such primitives are still largely a mystery to you, you shouldn’t stress yourself with topics such as optimization.

Please don’t do it. You’ll do more harm than good. Try practicing more and learning more about programming and object-orientedness, try to tackle all kinds of little experiments and projects. Just don’t be on the wrong side of the Dunning-Kruger curve.

Also here, where the docs explicitly state

But I don’t know if a better question would’ve been “what’s O(1)?”

@OP: In a nutshell, it means “it’s a punctual conclusion drawn correctly regardless of how many elements there are, which isn’t always the case”

You can learn more about the ‘big O notation’ on the internet. But in general, O(1) is the best case. It speaks nothing of the speed, just about what to expect from it once the elements grow in their numbers. In this particular case, however, we know that Count can’t be any faster, the way it’s implemented, so you can fully rely on it without making own copies (unless you do want to record its state for some reason), and you should feel encouraged to call it directly, because this will radically minimize your errors.

Just as an example, removing from a list is O(n), because the worst case for that usage scenario is that you remove the first element, which would then prompt the internal logic to shift all subsequent elements back one slot. This is only true for a list however, some other structure is likely better equipped to deal with it. A better part of programming is managing your data responsibly and looking for the most accessible and best-performing ways to organize everything, without having to run stuff that behaves O(n) or worse like O(nlogn), O(n^2), O(2^n), or even O(n!). Sometimes it’s just inevitable however, due to nature of cosmos itself or our limited knowledge, but that’s another topic.

Keep in mind that what I just described isn’t called “optimization”. This is the baseline expectation from a programmer. Optimization is something else.

Thanks that is what I was looking for. I figured as much. My level is good enough. It is ok to point and laugh, hell I’ll laugh with you. Moving on.

You can see what I have manged to accomplish with my meager programming skills, All of this I have built on my own from scratch with the exception of the Shields shader on the ships, which I bought and heavily modified.

https://www.youtube.com/watch?v=Sq9AW6fElJA

Anywho… have a good day everyone.

1 Like

That’s a weird reply because nobody is laughing.

But very good work though, keep it up! The point of my replies was to empower you further.

That looks great! Keep on asking the questions, it’s how we all learn.

Good luck with the project. :slight_smile: