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

  • SEARCH
  • Home
  • 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 6217583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:27:06+00:00 2026-05-24T07:27:06+00:00

I have a generic Dictionary that I am using as a cache in a

  • 0

I have a generic Dictionary that I am using as a cache in a threaded .NET project (C#). I will be doing a lot of reads on the dictionary (potentially hundreds or more per second at peak times).

I have a simple Get(int id) method that should return a CustomObject if it’s in the cache, or null otherwise.

My question:
Is it faster/more efficient to lock the dictionary, or just use a try/catch block?

Assuming the dictionary is stored in a variable named “dic”.

Lock Sample:

public CustomObject Get(int id)
{
   lock(dic)
   {
      if (dic.ContainsKey(id))
         return dic[id];
   }
   return null;
}

Try/Catch Sample:

public CustomObject Get(int id)
{
   try
   {
      return dic[id];
   }
   catch
   {
      return null;
   }
}
  • 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-24T07:27:09+00:00Added an answer on May 24, 2026 at 7:27 am

    I think you should test it in your own environment. Basically:

    • Lock is cheap
    • Try without getting an exception is cheap, maybe even cheaper then lock
    • Try and getting exception is very expensive

    So now the question is, how often you expect to have cache-miss, and therefore get an exception thrown. I would go for lock() as it’s execution time is not dependent on whether you will or not get cache-hit, which means it’s more predictable and measurable, while still – very cheap. I don’t think that hundreds hits per second would be any problem.

    Simple tests I’ve made indicate, that getting cache-miss with try/catch is very, very expensive.

    Edit:

    Simple test shows that:

    • try-no throw costs about 2ms for 100k retrieves
    • lock costs about 6ms
    • try-throw costs about 4seconds

    Which means, got for lock(), because it’s more efficient then try/catch if you’re getting more then 1 cache miss per few thousands tries, and it’s much more stable, being not depended on luck.

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

Sidebar

Related Questions

Say that i have a generic dictionary with data like this (I hope the
I have a class that inherits a generic dictionary and an inteface public class
I have a class that inherits from a generic dictionary as follows: Class myClass
Using a reference from... http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx I have a serializable generic dictionary. One thing I
I have to work on some code that's using generic lists to store a
I want to have a Dictionary that maps strings to generic lists of varying
I'm using C# in Visual Studio 2008 with .NET 3.5. I have a generic
I want to write in Delphi (2009 - so I have generic dictionary class)
I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance
Looking at System.Collections.Generic.Dictionary<TKey, TValue> , it clearly implements ICollection<KeyValuePair<TKey, TValue>> , but doesn't have

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.