相关疑难解决方法(0)

const和readonly有什么区别?

是什么区别const,并readonly和你使用一个比其他?

.net c# const constants readonly

1269
推荐指数
19
解决办法
39万
查看次数

使用锁定Dictionary <string,object>的键

我有一个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)

这会在法庭上举行还是失败?我只是希望字典中的每个值都被独立锁定,因此锁定(和更新)一个值不会锁定其他值.此外,我知道锁定将持续很长时间 - 但数据将无效,直到完全更新.

c# dictionary locking

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×2

.net ×1

const ×1

constants ×1

dictionary ×1

locking ×1

readonly ×1