Click here to Skip to main content
12,301,394 members (63,288 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 21-Feb-13 12:19pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Capacity Gets or sets the number of elements that the ArrayList can contain.

Count Gets the number of elements actually contained in the ArrayList.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100