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 6811105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:17:13+00:00 2026-05-26T20:17:13+00:00

I am creating a class that will have a Key-Value pairing of some sort.

  • 0

I am creating a class that will have a Key-Value pairing of some sort. Currently, I have something similar to the following:

private static Dictionary<Type, List<PropertyInfo>> PropertyCache { get; set; }

Is this the correct way of implementing in a thread-safe approach? For some reason there is something nagging me about the effect of static on the Dictionary in this case. If this is correct, is there an incorrect way that can be demonstrated so that I can avoid it in the rest of my code.

Also, should it be read-only (I am only ever going to add-remove things from the collection)?

If it makes a difference, the property will always be declared private.

Thanks in advance

Although tagged C#, answers in VB are ok, I am “Bi-lingual” so to speak

EDIT:

Following discussion with Jon Skeet in the comments of his answer, the usage will be:

// I will do this
PropertyCache.Add(typeof(string), new List<PropertyInfo>());
PropertyCache.Remove(typeof(string));

// I will never do this 
PropertyCache = new Dictionary<Type, List<PropertyInfo>>();

I will never iterate over the collection either, only access by key.

  • 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-26T20:17:13+00:00Added an answer on May 26, 2026 at 8:17 pm

    Making a member static makes it more likely to have cross-thread access than an instance member. Remember that static doesn’t mean something is safe for use by multiple threads, it means the object is shared between multiple instances of an object in which it is defined.

    Collections which implement the ICollection interface can be cast to access a SyncRoot you can use to lock an object to ensure only one thread at a time can execute a series of instructions that would cause the collection to change. Using SyncRoot for Multithreaded Applications

    lock(((ICollection)myObject).SyncRoot)
    {
        //Code that should be executed by only one concurrent thread
        //This is add/insert/remove/iterate/clear/etc.
    }
    

    In addition to this manual (or older-school way) there are concurrent objects available in .NET 4 which do basically this but with some other special checks. For the most part these objects are as optimized for performance as you can get for fully-safe objects. If you’re using a very controlled and small set of actions on the object (you have 1 add, 1 remove method and never enumerating the whole collection but just accessing specific, known entries) you could use the lighter-weight lock() example above to get better performance.

    You’ll see this especially when adding multiple objects to a collection at once. Using the concurrent objects each Add operation is atomic with locking and unlocking. If you run this in a tight loop you lose a bit of performance to acquiring locks over and over. If another thread is trying to read, the contention for access gets a bit higher. If you use the lock statement yourself, you can just acquire the lock, add the objects in a fast, tight loop and then release the lock. Threads wanting to access the object will wait a little longer but overall the operations should finish faster. Also keep in mind the differences are typically so low it’s not really worth it and would fall under the category of premature optimization in almost all cases.

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

Sidebar

Related Questions

Currently I have created a ABCFactory class that has a single method creating ABC
Just wondering how I do this, Im creating a class that will have my
im creating a Factory class that will contruct and return an object. I normally
When creating a class that has internal private methods, usually to reduce code duplication,
I am creating a website that will have articles; each article will have comments.
I'm creating a class that will manage a connection to a Firebird database. The
I have a high-level goal of creating a static utility class that encapsulates the
I'm in a data structures class and we have an assignment that includes creating
I am creating a class that determines which of number of registered WCF client
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}

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.