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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:40:06+00:00 2026-05-23T06:40:06+00:00

I have a lightly used dictionary which is hardly ever going to be read

  • 0

I have a lightly used dictionary which is hardly ever going to be read or updated since the individual items raise events and return their results with their event args. In fact the thread is always going to be updated with the same thread. I was thinking about adding a simple lock just to be safe. I was wondering if I can just place the lock in the get accessor. Does this work?

        Dictionary<string,Indicator> indicators = new Dictionary<string,Indicator>();
        Dictionary<string, Indicator> Indicators
        {
            get
            {
                lock (indicators)
                {
                    return indicators;
                }
            }
        }

        public void AddIndicator(Indicator i)
        {
            lock (indicators)
            {
                indicators.Add(i.Name, i);
            }
        }
  • 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-23T06:40:07+00:00Added an answer on May 23, 2026 at 6:40 am

    That doesn’t do anything particularly useful, no.

    In particular, if you have:

    x = foo.Indicators["blah"]
    

    then the indexer will be executed without the thread holding the lock… so it’s not thread-safe. Think of the above code like this:

    Dictionary<string, Indicator> indicators = foo.Indicators;
    // By now, your property getter has completed, and the lock has been released...
    x = indicators["blah"];
    

    Do you ever need to do anything with the collection other than access it via the indexer? If not, you might want to just replace the property with a method:

    public Indicator GetIndicator(string name)
    {
        lock (indicators)
        {
            return indicators[name];
        }
    }
    

    (You may want to use TryGetValue instead, etc – it depends on what you’re trying to achieve.)

    Personally I’d prefer to use a reference to a privately-owned-and-otherwise-unused lock object rather than locking on the collection reference, but that’s a separate matter.

    As mentioned elsewhere, ConcurrentDictionary is your friend if you’re using .NET 4, but of course it’s not available prior to that 🙁

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

Sidebar

Related Questions

Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I have some ad-hoc reporting users hitting some SQL Server views. Occasionally the read
I have already read this web page http://api.jquery.com/end/ but I am still clueless on
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
Have an app that can use tts to read text messages. It can also
Have just started to get into CakePHP since a couple of weeks back. I
Have the following scenario. I have a few form, which essentially have a few

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.