troubleshooting Question

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

Avatar of John
JohnFlag for United Kingdom of Great Britain and Northern Ireland asked on
* hashtableVisual Basic.NET.NET ProgrammingC#
18 Comments5 Solutions369 ViewsLast Modified:
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

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)

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

Freelance programmer / Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 5 Answers and 18 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 5 Answers and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros