c# - Why can't I preallocate a hashset<T> -


why can't preallocate hashset<t>?

there times when might adding lot of elements , want eliminate resizing.

there's no technical reason why shouldn't possible - microsoft hasn't chosen expose constructor initial capacity.

if can call constructor takes ienumerable<t> , use implementation of icollection<t>, believe use size of collection initial minimum capacity. implementation detail, mind you. capacity has large enough store distinct elements...

edit: believe if capacity turns out way larger needs be, constructor trim excess when it's finished finding out how many distinct elements there really are.

anyway, if have collection you're going add hashset<t> and implements icollection<t>, passing constructor instead of adding elements 1 one going win, :)

edit: 1 workaround use dictionary<tkey, tvalue> instead of hashset<t>, , not use values. won't work in cases though, won't give same interface hashset<t>.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -