I have inherited some C# code that I need to do fine-tuning. Since the dictionary (in the following code) is created on stack meaning individual instance (created by different threads) will be used for each call and it is not necessary to use the lock in this case, is that correct? Looks to me, it is not necessary.
private object textLock = new object();
private Dictionary<string, string> GetMyTexts(Language language)
{
Dictionary<string, string> texts = new Dictionary<string, string>();
foreach (KeyValuePair<string, DisplayText> pair in Repository.DisplayTextCollection.Texts)
{
string value = pair.Value.Get(language);
//other code ....
lock(textLock)
{
texts.Add(pair.Key, value);
}
}
return texts;
}