Lazy initialization is only required for resource intensive objects. A list is not. Maybe the elements it contains. A list autoscales. It starts with room reserved for little references. If it needs more room it reserves more space.
It is also worth mentioning that your example is not thread safe. It's better to refer to the Lazy<T> Class
:
https://msdn.microsoft.com/en-us/library/dd642331.aspx
Yes, for 90+% of use-cases I agree with you.
However, for performance oriented cases:
it is still better to not create an empty list and not to use it's auto-scale if possible (e.g. if you know for sure there are never more than X objects in it). The core reason of this is .NET Garbage Collection.
The more instances it has to collect, the longer the GC pauses are. Also the more memory areas it has to move from Gen0 to Gen1 or has to consolidate, the longer it takes.
If performance is really that critical, then it might be better to go with Span from the beginning, a good blog post about it can be found here: http://adamsitnik.com/Span/
Anyway, always do micro benchmarks for segments where you want to optimize performance in that level. And avoid premature optimization, it's often better to have a function up and running with tests, before doing them.
Also 2 things to be aware of with
Lazy<T>
:Activator.CreateInstance
, which is known for bad performance.Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit