Why can’t I preallocate a hashset<T>?
There are times when i might be adding a lot of elements to it and i want to eliminate resizing.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Answer below was written in 2011. It’s now in .NET 4.7.2 and .NET Core 2.0; it will be in .NET Standard 2.1.
There’s no technical reason why this shouldn’t be possible – Microsoft just hasn’t chosen to expose a constructor with an initial capacity.
If you can call a constructor which takes an
IEnumerable<T>and use an implementation ofICollection<T>, I believe that will use the size of the collection as the initial minimum capacity. This is an implementation detail, mind you. The capacity only has to be large enough to store all the distinct elements…EDIT: I believe that if the capacity turns out to be way larger than it needs to be, the constructor will trim the excess when it’s finished finding out how many distinct elements there really are.
Anyway, if you have the collection you’re going to add to the
HashSet<T>and it implementsICollection<T>, then passing it to the constructor instead of adding the elements one by one is going to be a win, basically 🙂EDIT: One workaround would be to use a
Dictionary<TKey, TValue>instead of aHashSet<T>, and just not use the values. That won’t work in all cases though, as it won’t give you the same interface asHashSet<T>.