C
C#5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
ikalou.
ikalou.OP5mo ago
I think the docs say new List<T> starts at 0 (instead of 4)?
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
SleepWellPupper
SleepWellPupper5mo ago
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
MODiX5mo 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
SleepWellPupper5mo ago
So it will grow accordingly
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
ikalou.
ikalou.OP5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic5mo ago
No
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic5mo ago
if you wrote new List<T>() the the parameterless constructor is what you get
SleepWellPupper
SleepWellPupper5mo ago
Dropdown on the right "run" can be switched to "C#" to show the lowered code
SleepWellPupper
SleepWellPupper5mo ago
namely:
No description
ikalou.
ikalou.OP5mo 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 User5mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic5mo ago
you should use a collection expression
ikalou.
ikalou.OP5mo 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
reflectronic5mo ago
it is meant to address this problem, among other things List<int> x = [1, 2, 3];
ikalou.
ikalou.OP5mo ago
But SleepWellPupper's screenshot shows that this is not the case
reflectronic
reflectronic5mo ago
that’s because that’s not a collection expression
ikalou.
ikalou.OP5mo ago
I see. I though that's what it was called.
reflectronic
reflectronic5mo ago
that’s a collection initializer a collection expression is this
MODiX
MODiX5mo 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.OP5mo ago
I'm using Unity and I dont think it has support for collection expressions unfortunately.
No description
SleepWellPupper
SleepWellPupper5mo ago
So use the language version preview/latest
Petris
Petris5mo ago
note that it's different on main of roslyn
SleepWellPupper
SleepWellPupper5mo ago
how does it differ?
Petris
Petris5mo ago
you can check it there yourself
No description
SleepWellPupper
SleepWellPupper5mo ago
wowza that is noice thanks for the hint :)
Aaron
Aaron5mo ago
you can't unity bundles their own Roslyn stuck on a subset of C#9
SleepWellPupper
SleepWellPupper5mo ago
yuck sorry to hear
Petris
Petris5mo ago
iirc it has roslyn 3.9
Want results from more Discord servers?
Add your server