List, Collection Initializer and Capacity
Hi! Does creating a new List with new List<T> { new T() } (or any number of elements > 0) starts with the proper capacity in order to avoid resizes or does it start at 0 and then grows to add the elements and I should be using new List<T>(1) { new T() } (for instance) instead? Thanks for helping me figure this out!
43 Replies
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
I think the docs say new List<T> starts at 0 (instead of 4)?
TeBeCo
REPL Result: Success
Result: int
Compile: 204.168ms | Execution: 17.922ms | React with ❌ to remove this embed.
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
REPL Result: Success
Result: int
Compile: 268.383ms | Execution: 25.126ms | React with ❌ to remove this embed.
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
REPL Result: Success
Result: int
Compile: 234.006ms | Execution: 27.054ms | React with ❌ to remove this embed.
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
gets turned into
To answer your question
TeBeCo
REPL Result: Success
Result: int
Compile: 338.572ms | Execution: 21.330ms | React with ❌ to remove this embed.
So it will grow accordingly
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
My question is‚ when using a collection initializer with n elements‚ is new List<T>() {...} be as efficient as new List<T>(n) {...}. It would seem logical but I can't find the information nor do I know how to test it.
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
REPL Result: Success
Result: int
Quoted by
<@689473681302224947> from #List, Collection Initializer and Capacity (click here)
Compile: 268.383ms | Execution: 25.126ms | React with ❌ to remove this embed.