user2387900
user2387900

Reputation: 215

Capacity property in ArrayList

I am learning the properties of Arraylist in C#. I went through the code, and implemented a simple arraylist. Below is the code which I tried.

ArrayList newal= new ArrayList();
newal.Add( "Alice");
newal.Add( "Wonderland");
newal.Add( "Dreamland");
Console.WriteLine( "Capacity:{0}",newal.Capacity );

Here the answer comes as count =3 (if i calculate), which i understand, but the capacity comes as 4. How does it calculate it as 4 ? Does it include null by default ?

I do not know, by the end is it calculating something ?

Upvotes: 1

Views: 8516

Answers (4)

user7739219
user7739219

Reputation: 1

When you Add one element to the ArrayList it will creat the max memory space for 4 elements, and next if u add morethan 4 elements then it will automatically increase it to 8 and goes on.

ex: ArrayList arr = new ArrayList();

  arr.Add("xyz");
  arr.Add(23);
  arr.Add("abc"); 

For this it will shows the capacity as 4 and count as 3 and For this array if you add 2 more elements then capacity will be doubled that is 8 and count is 5.

Upvotes: 0

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Count property tells how many elements are currently in ArrayList where as Capacity property tells how many elements can fit into ArrayList without allocating more memory.

How does it calculate it as 4 ?

When you add an element to List it will check the Capacity whether the element can fit or not. If not it will just "Pre-Allocate" the ArrayList capacity to double of its current Capacity.

So in your example It is 4 since when first element is added Initial Capacity will be set to 4. You can test this by adding more elements to it. After adding 5 elements List Capacity will be 8 and so on.

Hope this helps

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460360

The Capacity is not the same as the Count. The former is the size of the internal backing array, so how many items can this array hold until it needs to be recreated. The latter is just the count of current items.

A doubling algorithm increases the size of the internal array if required:

EnsureCapacity checks this on ArrayList.Add, so it's 4 at the minimum (ILSpy, .NET 4)

// System.Collections.ArrayList
private void EnsureCapacity(int min)
{
    if (this._items.Length < min)
    {
        int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
        if (num < min)
        {
            num = min;
        }
        this.Capacity = num;
    }
}

MSDN:

Capacity is the number of elements that the ArrayList can store. Count is the number of elements that are actually in the ArrayList. 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.

Upvotes: 2

Haris Hasan
Haris Hasan

Reputation: 30127

Capacity and Count represent two different things

Count returns the number of items

Capacity tells the maximum number of items ArrayList can currently hold. Capacity will increase automatically when you will put more items in the ArrayList

Upvotes: 3

Related Questions