Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 386295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:33:46+00:00 2026-05-12T15:33:46+00:00

I am trying to optimize a piece of .NET 2.0 C# code that looks

  • 0

I am trying to optimize a piece of .NET 2.0 C# code that looks like this:

Dictionary<myType, string> myDictionary = new Dictionary<myType, string>();
// some other stuff
// inside a loop check if key is there and if not add element
if(!myDictionary.ContainsKey(currentKey))
{
   myDictionary.Add(currentKey, "");
}

Looks like the Dictionary has been used by whoever wrote this piece of code even if not needed (only the key is being used to store a list of unique values) because faster than a List of myType objects for search.
This seems obviously wrong as only the key of the dictionary but I am trying to understand what’s the best way to fix it.

Questions:

1) I seem to understand I would get a good performance boost even just using .NET 3.5 HashSet. Is this correct?

2) What would be the best way to optimize the code above in .NET 2.0 and why?

EDIT:
This is existing code I am trying to optimize, it’s looping through dozens of thousands items and for each one of them is calling a ContainsKey. There’s gotta be a better way of doing it (even in .NET 2.0)! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:33:46+00:00Added an answer on May 12, 2026 at 3:33 pm

    I think you need to break this down into 2 questions

    Is Dictionary<myType,string> the best available type for this scenario

    No. Based on your breakdown, HashSet<myType> is clearly the better choice because it’s usage pattern more accurately fits the scenario

    Will switching to Hashset<myType> give me a performance boost?

    This is really subjective and only a profiler can give you the answer to this question. Likely you’ll see a very minor memory size improvement per element in the collection. But in terms of raw computing power I doubt you’ll see a huge difference. Only a profiler can tell you if there is one.

    Before you ever make a performance related change to your code remember the golden rule.

    Don’t make any performance related changes until a profiler has told you precisely what is wrong with your code.

    Making changes which violate this rule are just guesses. A profiler is the only way to measure success of a performance fix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to optimize a piece of code that clones an object: #region
I was tying to optimize a piece of code that has this construct: while
I have been trying to optimize some code which handles raw pixel data. Currently
I'm trying to optimize code that I have. In order to do that, I
I'm trying to optimize and existing piece of WebGl JavaScript code, and the bottleneck
I am trying to optimize a small piece of code with SSE intrinsics (I
I'm trying to optimize my WPF/VB.net program, and I'm considering freezing some objects. However,
I am trying to optimize some python code (to speed up some matrix operations),
I'm trying to optimize some existing code ( where speed is the primary criteria
I am trying to optimize some stored procedures on a SQL Server 2000 database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.