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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:02:18+00:00 2026-05-10T16:02:18+00:00

I have a Dictionary<string, someobject> . EDIT: It was pointed out to me, that

  • 0

I have a Dictionary<string, someobject>.

EDIT: It was pointed out to me, that my example was bad. My whole intention was not to update the references in a loop but to update different values based on differnt threads need to update/get the data. I changed the loop to a method.

I need to update items in my dictionary – one key at a time and i was wondering if there are any problems in using the lock on the .key value of my Dictionary object?

private static Dictionary<string, MatrixElement> matrixElements = new Dictionary<string, MatrixElement>();  //Pseudo-code  public static void UpdateValue(string key) {     KeyValuePair<string, MatrixElement> keyValuePair = matrixElements[key];     lock (keyValuePair.Key)     {         keyValuePair.Value  = SomeMeanMethod();     } } 

Would that hold up in court or fail? I just want each value in the dictionary to be locked independantly so locking (and updating) one value does not lock the others. Also i’m aware the locking will be holding for a long time – but the data will be invalid untill updated fully.

  • 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. 2026-05-10T16:02:19+00:00Added an answer on May 10, 2026 at 4:02 pm

    Locking on an object that is accessible outside of the code locking it is a big risk. If any other code (anywhere) ever locks that object you could be in for some deadlocks that are hard to debug. Also note that you lock the object, not the reference, so if I gave you a dictionary, I may still hold references to the keys and lock on them – causing us to lock on the same object.

    If you completely encapsulate the dictionary, and generate the keys yourself (they aren’t ever passed in, then you may be safe.

    However, try to stick to one rule – limit the visibility of the objects you lock on to the locking code itself whenever possible.

    That’s why you see this:

    public class Something {   private readonly object lockObj = new object();    public SomethingReentrant()   {     lock(lockObj)    // Line A     {       // ...      }    } } 

    rather than seeing line A above replaced by

      lock(this) 

    That way, a separate object is locked on, and the visibility is limited.

    Edit Jon Skeet correctly observed that lockObj above should be readonly.

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

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Use a data template with something like: <ListBox.ItemTemplate> <DataTemplate> <TextBlock… May 11, 2026 at 9:47 am
  • added an answer To defend from SQL injection use prepared statements if possible.… May 11, 2026 at 9:47 am
  • added an answer $(document).ready() and pageLoad() are not the same! http://encosia.com/2009/03/25/document-ready-and-pageload-are-not-the-same/ From the… May 11, 2026 at 9:47 am

Related Questions

I have a Dictionary<string, someobject> . EDIT: It was pointed out to me, that
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I have a ComponentResourceKey defined in my resource dictionary like this: <Style x:Key={ComponentResourceKey TypeInTargetAssembly={x:Type
A lot of the collection classes in .Net (i.e., List<T>, Dictionary<TKey, TValue>) have an
I have a dictionary of 200,000 items (the keys are strings and the values
I have a Dictionary where I hold data for movieclips, and I want the
I have a dictionary that I normally access with a key, so I need
I have a dictionary where keys are strings, and values are integers. stats =
I have a Dictionary that when I add multiple values to it, the items

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.