+ 3

Difference between capacity and count

using System; using System.Collections.Generic; class Geeks {     // Main Method     public static void Main(String[] args)     {         // Creating a List of integers         // Here we are not setting         // Capacity explicitly         List<int> firstlist = new List<int>();         // adding elements in firstlist         firstlist.Add(1); firstlist.Add(2); firstlist.Add(3); firstlist.Add(4); // Printing the Capacity of firstlist Console.WriteLine("Capacity Is: " + firstlist.Capacity); // Printing the Count of firstlist Console.WriteLine("Count Is: " + firstlist.Count); // Adding some more // elements in firstlist firstlist.Add(5); firstlist.Add(6); // Printing the Capacity of firstlist // It will give output 8 as internally // List is resized Console.WriteLine("Capacity Is: " + firstlist.Capacity); How does the capacity change from 4 to 8? Explain

12th Nov 2023, 12:42 PM
safir safir1
safir safir1 - avatar
6 Antworten
+ 9
"Capacity is the number of elements that the List<T> can store before resizing is required, whereas Count is the number of elements that are actually in the List<T>. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements." Source: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.capacity?view=net-7.0
12th Nov 2023, 1:12 PM
Tibor Santa
Tibor Santa - avatar
+ 4
safir safir1 read Tibor Santa's answer carefully. The Add method checks whether the list can hold the new element. If the capacity is too small, then Add automatically increases the capacity.
12th Nov 2023, 4:24 PM
Brian
Brian - avatar
+ 3
safir safir1 you should read also, not just write. It was already mentioned that capacity allocation is automatic. So there will be a specific rule for it. For the programmer who uses the list, the exact algorithm is almost always irrelevant. But you can read about it in this thread. https://stackoverflow.com/questions/2247773/is-it-worthwhile-to-initialize-the-collection-size-of-a-listt-if-its-size-rea
13th Nov 2023, 8:38 AM
Tibor Santa
Tibor Santa - avatar
+ 2
There are various strategies for automatic allocation, which you could research. I know that Microsoft has developed an ingenious approach to .Net memory management in general, but specifically for this case I do not know their rules. The article that was linked above mentions that, if you find the capacity is too large, you can trim it to the exact count of elements by calling the TrimExcess() method.
13th Nov 2023, 8:23 AM
Brian
Brian - avatar
0
I know that in the above code, the list resized , but I did not understand how does capacity change from size 4 to 8. Please explain this
12th Nov 2023, 3:08 PM
safir safir1
safir safir1 - avatar
0
Thanks for your answer Brain So does the capacity increase automatically? Is there a special rule behind it or not? (I mean in this amount increase)
13th Nov 2023, 6:32 AM
safir safir1
safir safir1 - avatar