Using lock on the key of a Dictionary

后端 未结 7 1047
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 17:18

I have a Dictionary.

EDIT: It was pointed out to me, that my example was bad. My whole intention was not to update the references

相关标签:
7条回答
  • 2021-02-15 18:24

    I can see a few potential issues there:

    1. strings can be shared, so you don't necessarily know who else might be locking on that key object for what other reason
    2. strings might not be shared: you may be locking on one string key with the value "Key1" and some other piece of code may have a different string object that also contains the characters "Key1". To the dictionary they're the same key but as far as locking is concerned they are different objects.
    3. That locking won't prevent changes to the value objects themselves, i.e. matrixElements[someKey].ChangeAllYourContents()
    0 讨论(0)
提交回复
热议问题