是什么区别const,并readonly和你使用一个比其他?
我有一个Dictionary<string, someobject>.
编辑:有人向我指出,我的榜样很糟糕.我的全部意图不是更新循环中的引用,而是根据需要更新/获取数据的不同线程更新不同的值.我将循环更改为方法.
我需要更新我的字典中的项目 - 一次一个键,我想知道在我的Dictionary对象的.key值上使用锁是否有任何问题?
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这会在法庭上举行还是失败?我只是希望字典中的每个值都被独立锁定,因此锁定(和更新)一个值不会锁定其他值.此外,我知道锁定将持续很长时间 - 但数据将无效,直到完全更新.