Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Lists Lists

Carel Du Plessis
PLUS
Carel Du Plessis
Courses Plus Student 16,356 Points

I don't see the 3 times it resizes the List

Can someone point out where the list resizes the third time (in the question below)

Question: How many times will the list need to resize while the following code runs?

List favoriteThings = new List();

favoriteThings.Add("sunshine");
favoriteThings.Add("ice cream");
favoriteThings.Add("babies laughing");
favoriteThings.Add("chocolate chip cookies");
favoriteThings.Add("Star Trek");
favoriteThings.Add("bean bag chairs");
favoriteThings.Add("apples");
favoriteThings.Add("snow");
favoriteThings.Add("mountain streams");
favoriteThings.Add("trees");

Answer: 3 times

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello, Carel

The array is resized in this order:

favoriteThings.Add("sunshine");
The first time we add an item, the list is resized to a capacity of 4.

favoriteThings.Add("Star Trek");
Adding a 5th item causes a resize so that the capacity doubles to 8.

favoriteThings.Add("mountain streams");
The final resize happens because we've now asked the list to add a 9th item when it only has a capacity of 8. The resize results in a capacity of 16.

I hope this helps.

GOT IT! My first thought was, but where is the original list? After reading your explanation, and looking again it hit me, its not about what was added first, well, that too, but this question is more about the process! Yes, my brain got a kick start! Thanks so much Justin!