Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
ArrayList alist = new ArrayList();
alist.Add(0);
Console.WriteLine(alist.Count);
Console.WriteLine(alist.Capacity);

what is different between this two property Capacity and count ?
Posted

Solution 1 would be correct if it explained Capacity. It does not mean that you cannot add more items then Capacity. You can. But all you need is to read the documentation:

MSDN tells us:
Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.


More importantly: you should never use this list type. It is rendered obsolete as early as of the .NET v.2.0, when generics were introduces. You should always use generic collection types from System.Collections.Generic (and also specialized collections). For a list, use System.Collections.Generic.List<>:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^].

Non-generic types are much worse because they require potentially dangerous type cast. You never need them. However, these classes were not marked with [Obsolete] attribute, just because there is nothing wrong with using them in legacy code. In new development, there is absolutely no reason to use them.

—SA
 
Share this answer
 
Capacity Gets or sets the number of elements that the ArrayList can contain.

Count Gets the number of elements actually contained in the ArrayList.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-13 18:33pm    
The answer is confusing, in part of Capacity. Microsoft writes "can contain", but its the best not to compile these words, because they suggests that Count cannot be more than Capacity. This is just bad Microsoft wording, the documentation actually explains how it works. (I did not vote this time.)

Please see my answer.
—SA
DinoRondelly 21-Feb-13 18:40pm    
I just did the guy the favor of googling arraylist, looking at the msdn and copying and pasting what they had there

http://msdn.microsoft.com/en-ca/library/system.collections.arraylist.aspx

gotta be worth atleast a 4, he could of just used the google his self, :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900