We help IT Professionals succeed at work.
Get Started

.net programming hash set, hashtable, dictionary, List, Which should I use?  I amusing VB.net but C# is OK

Last Modified: 2018-04-27
I am writing an application where performance is quite important.

I need to store templates in memory.  A template is an array of integers.  

I was trying to use recordsets, but they seem pretty expensive to use and I've had great performance increases from using arrays so I decided to try that.  

Each Template has an ID which is an integer (0  -  65535)

I created a structure for a template and created an array whose data type is that structure.  

Public Structure udtTemplateRecord
        Dim TemplateType As UShort 
        Dim Count As UShort
        Dim Count2 As UShort
        Dim Type() As UShort
        Dim Length() As UShort
    End Structure

Dim Array(65535) as udtTemplateRecord

Open in new window

I was going to do this to look up a template (the lookup is where performance is important)

Dim Template as udtTemplateRecord
Template = Array(TemplateID)

Open in new window

While lookups should be pretty much instant as I am simply referencing an array by index, I realised that this will start to consume a lot of memory, especially when I start to have many of these structures in memory.  

Also it is inefficient as there will probably never be more than a 50-200 templates in memory at any one time (often as few as 3 or 4), but the array would be large (65535).  

Then I saw mention of a hashtable and I read this as only storing a template when I need it, rather than allocating space for every possible template ID but being optimised for lookups.  Some documentation pointed out that a dictionary object is newer/better.  Then I came across Hash Sets and lists.

I have never had to work on in-memory data like this before and usually performance of this part of a program is not important.  However it is this time.  

To be fair, I haven't a clue about pro's and cons of the above and don't want to spend a day or two reading about them all and trying to get code to work.  

I'd love a brief note on which structure (hash set, hashtable, dictionary, list etc) is best to use (with lookup performance being key).  I don't need to iterate through the data, sort it, filter it etc, simply look a template by it's ID (Integer) and return a structure .  

A bit of code to get me going would be super useful.  

Thanks in advance

Watch Question
Freelance programmer / Consultant
This problem has been solved!
Unlock 5 Answers and 18 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE