C
C#2y ago
ikalou.

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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ikalou.
ikalou.OP2y ago
I think the docs say new List<T> starts at 0 (instead of 4)?
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
(new List<int>()).Capacity
(new List<int>()).Capacity
Result: int
0
0
Compile: 204.168ms | Execution: 17.922ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
(new List<int>() {1}).Capacity
(new List<int>() {1}).Capacity
Result: int
4
4
Compile: 268.383ms | Execution: 25.126ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
(new List<int>(3) {1}).Capacity
(new List<int>(3) {1}).Capacity
Result: int
3
3
Compile: 234.006ms | Execution: 27.054ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
SleepWellPupper
var l = new List<T>()
{
new T(),
new T(),
new T()
}
var l = new List<T>()
{
new T(),
new T(),
new T()
}
gets turned into
var l = new List<T>();
l.Add(new T());
l.Add(new T());
l.Add(new T());
var l = new List<T>();
l.Add(new T());
l.Add(new T());
l.Add(new T());
To answer your question
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
var l = new List<int>();
l.Add(1);
l.Capacity
var l = new List<int>();
l.Add(1);
l.Capacity
Result: int
4
4
Compile: 338.572ms | Execution: 21.330ms | React with ❌ to remove this embed.
SleepWellPupper
So it will grow accordingly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ikalou.
ikalou.OP2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
(new List<int>() {1}).Capacity
(new List<int>() {1}).Capacity
Result: int
4
4
Compile: 268.383ms | Execution: 25.126ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
TeBeCo
REPL Result: Success
(new List<int>() {1,2,3,4,5}).Capacity
(new List<int>() {1,2,3,4,5}).Capacity
Result: int
8
8
Compile: 235.112ms | Execution: 25.023ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic2y ago
No
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic2y ago
if you wrote new List<T>() the the parameterless constructor is what you get
SleepWellPupper
Dropdown on the right "run" can be switched to "C#" to show the lowered code
SleepWellPupper
namely:
No description
ikalou.
ikalou.OP2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic2y ago
you should use a collection expression
ikalou.
ikalou.OP2y ago
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
reflectronic
reflectronic2y ago
it is meant to address this problem, among other things List<int> x = [1, 2, 3];
ikalou.
ikalou.OP2y ago
But SleepWellPupper's screenshot shows that this is not the case
reflectronic
reflectronic2y ago
that’s because that’s not a collection expression
ikalou.
ikalou.OP2y ago
I see. I though that's what it was called.
reflectronic
reflectronic2y ago
that’s a collection initializer a collection expression is this
MODiX
MODiX2y ago
reflectronic
sharplab.io (click here)
List<int> l =
[
1,
2,
3
];
Console.WriteLine(l.Capacity);
List<int> l =
[
1,
2,
3
];
Console.WriteLine(l.Capacity);
React with ❌ to remove this embed.
ikalou.
ikalou.OP2y ago
I'm using Unity and I dont think it has support for collection expressions unfortunately.
No description
SleepWellPupper
So use the language version preview/latest
Petris
Petris2y ago
note that it's different on main of roslyn
SleepWellPupper
how does it differ?
Petris
Petris2y ago
you can check it there yourself
No description
SleepWellPupper
wowza that is noice thanks for the hint :)
Aaron
Aaron2y ago
you can't unity bundles their own Roslyn stuck on a subset of C#9
SleepWellPupper
yuck sorry to hear
Petris
Petris2y ago
iirc it has roslyn 3.9

Did you find this page helpful?