15

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?

Setyo N
  • 1,953
  • 2
  • 26
  • 28
  • The Queue and Stack both have Count properties which are similar to the capacity, no idea why they haven't included the capacity property though. http://msdn.microsoft.com/en-us/library/fy0wwyz4.aspx – JayH Jun 27 '13 at 08:39
  • 1
    @JamieHennerley: Count and Capacity is *not* the same. `Count` is the amount of items you stored in the structure. `Capacity` is the amount of items the internal array can store. `Capacity` is never smaller than `Count`. Normally, it is bigger than `Count`. If it equals `Count` and you want to add another item, the internal array gets copied into a new array with a bigger capacity. – Daniel Hilgarth Jun 27 '13 at 08:41

3 Answers3

6

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...)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 3
    There's also TrimExcess() which sets the capacity to the current count if the internal array is not at least 90% full. – drch Jun 27 '13 at 08:57
3

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.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • 1
    But Stack and Queue both have `Count` properties... The `Capacity` property exists only for optimization purposes (which would presumably also apply to Stack and Queue as well as List). – Matthew Watson Jun 27 '13 at 08:44
  • That's a weird one. Both stacks and queues are commonly implemented using linked lists (where a capacity wouldn't help), but that's an example of an implementation detail leaking out. Perhaps that's the point, if `Capacity` was defined that would give a big clue on the underlying implementation. – Jeff Foster Jun 27 '13 at 09:23
  • Well, you *can* set the initial capacity of List and Queue by using the right constructor. The parameter name is even `capacity`... – Matthew Watson Jun 27 '13 at 09:30
  • `List` is a concrete type that hints at the underlying implementation ("Represents a strongly typed list of objects that can be accessed by index") so that seems fine to have a capacity. `LinkedList` on the other hand, doesn't provide such a constructor. – Jeff Foster Jun 27 '13 at 09:43
  • 2
    Indeed, but the point is that `Stack` and `Queue` *do* allow you to specify a capacity. – Matthew Watson Jun 27 '13 at 10:15
2

This information is not exposed by Stack<T> or Queue<T>. This information isn't even stored explicitly in those classes, only implicitly in form of the length of the internal array.

Your only option to get that would be to use reflection to access the array and get it's length.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443