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•4mo 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•4mo 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•4mo 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•4mo 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•4mo 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•4mo 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.
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
REPL Result: Success
Result: int
Compile: 235.112ms | Execution: 25.023ms | React with ❌ to remove this embed.
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
No
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
if you wrote
new List<T>()
the the parameterless constructor is what you getDropdown on the right "run" can be switched to "C#" to show the lowered code
namely:
Thanks everyone. So I should specify the capacity manually in the constructor to accommodate for the number of elements if the collection initializer if the code is in a hot path.
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
you should use a collection expression
I was thinking maybe when using a collection initializer the compiler (or whatever component has this job) might have set the capacity implicit to minimize operations
it is meant to address this problem, among other things
List<int> x = [1, 2, 3];
But SleepWellPupper's screenshot shows that this is not the case
that’s because that’s not a collection expression
I see. I though that's what it was called.
that’s a collection initializer
a collection expression is this
I'm using Unity and I dont think it has support for collection expressions unfortunately.
So use the language version preview/latest
note that it's different on main of roslyn
how does it differ?
you can check it there yourself
wowza that is noice
thanks for the hint :)
you can't
unity bundles their own Roslyn stuck on a subset of C#9
yuck
sorry to hear
iirc it has roslyn 3.9