Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
var nums = new List<int>();
Console.WriteLine(nums.Capacity);
for (int i = 0; i < 5; i++)
{
nums.Add(i);
}
Console.WriteLine(nums.Count);
Console.WriteLine(nums.Capacity);0 5 8
- Expecting
Capacityto equalCount: a list keeps spare places and doubles its array when it is full, so after 5 elements it has 8 places. - Expecting a new list to start with 4 places:
new List<int>()has none, and the firstAddcreates an array of 4.
- What do
CountandCapacityprint after one more line,nums.RemoveAt(0);? - How does the output change if the list is created as
new List<int>(10), and why?