I have a static readonly dictionary. No modifications will be made to this dictionary.
I have multiple threads reading from this dictionary using the .ContainsKey(Key). e.g.
class MyData { private static private IDictionary<int, string> _dictionary = new Dictionary<int, string>(); MyData() { // Load Dictionary here } public string GetValue(int key) { if (_dictionary.ContainsKey(key)) { return _dictionary[key]; } } }
Are there any threading problems with doing this?
If nobody is mutating it: this is fine. If there were occasional edits, then perhaps look at ReaderWriterLockSlim, or (my preference) edit a snapshot/copy and swap the reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With