I am wondering if there is difference (in the aspect of performance, memory saving) to define a list with a default size or specify one.
List<object> m_objects = new List<object>();
or
List<object> m_objects = new List<object>(100);
Both will increase the size by doubling up if more items are added, right?
Thanks,
If you know that you will have more than 100 items, the second one is faster.
Every time it "doubles up", it needs to copy the contents of the entire existing array. For large lists, this can be slow.
If you specify the capacity, it won't need to resize at all until it gets bigger than what you specified.
If you never add more than 100 items, it justs wastes a bit of memory (specifically, IntPtr.Size * (Capacity - Count)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With