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