Is Capacity property more useful in a List than in the other collections such as Stack and Queue? Or is there another way to get the capacity of a Stack or a Queue?
We use stack or queue instead of arrays/lists when we want the elements in a specific order i.e. in the order we put them (queue) or in the reverse order (stack). Queues and stacks are dynamic while arrays are static. So when we require dynamic memory we use queue or stack over arrays.
Queue is significantly faster than List , where memory accesses are 1 vs. n for List in this use case. I have a similar use case but I have hundreds of values and I will use Queue because it is an order of magnitude faster. A note about Queue being implemented on top of List : the key word is "implemented".
Queue is completely full when rear is at last array position i.e. (MaxSize -1). Now no more elements can be inserted in queue even if the queue has some empty spaces. This is a drawback of simple queues.
Whenever you add an item >=length, the capacity is doubled. As you said, this does limit it to the same limitations as an array in terms of # of elements, which is actually normally 2,146,435,071 elements (not 2^31) for everything except byte values.
I think that the reason that List
has a Capacity
property and Stack
and Queue
do not is that the normal usage of those types is different.
For a List
it is fairly common to populate it with a large set of values, even some time after it has been created. Providing the Capacity
property (and constructor argument) helps to mitigate the number of reallocations that would be done when adding a large number of items to the list.
Stack
and Queue
on the other hand do not tend to have large numbers of items added to them at once after they've been created.
Presumably Microsoft decided that it wasn't worth adding the Capacity
property because it wouldn't be used very much.
However, do note that Queue does have a constructor that allows you to specify an initial capacity, and so does Stack.
Also note that both classes also have a TrimExcess()
method, as mentioned by @drch below.
So Microsoft thought it would be useful at construction time, but not useful later on - so they only added the capacity functionality to the constructors.
(Incidentally I've just had a quick check through our code base, and it seems that the only time we use a capacity for List
is in fact at construction time. So maybe if Microsoft were designing List now, they might also omit the Capacity
property for List...)
Stack
and Queue
are LIFO and FIFO structures respectively.
In both cases, you (as a consumer of the API) generally only need to know how to put data into the structure, and how to get data out again. You aren't concerned with the length of the data structure, only with push
and pop
.
If you need to get the capacity for any reason (a bounded stack/queue perhaps?) then it'd probably be better to hide that detail from the end user and implement your own stack/queue structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With