c# - How does IEnumerable<T>.ToArray() work? -


is two-pass algorithm? i.e., iterates enumerable once count number of elements can allocate array, , pass again insert them?

does loop once, , keep resizing array?

or use intermediate structure list (which internally resizes array)?

it uses intermediate structure. actual type involved buffer, internal struct in framework. in practice, type has array, copied each time full allocate more space. array starts length of 4 (in .net 4, it's implementation detail might change), might end allocating , copying lot when doing toarray.

there optimization in place, though. if source implementes icollection<t>, uses count allocate correct size of array start.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -