gpt4 book ai didi

c# - 并发字典 AddOrUpdate 方法第三个参数?

转载 作者:太空狗 更新时间:2023-10-29 22:07:27 26 4
gpt4 key购买 nike

private readonly ConcurrentDictionary<string, System.Drawing.Color> _colorSet;      

public void BuildColorSet(IList<string> colorNames, string prefix, bool forceLastToGray)
{
var size = forceLastToGray ? colorNames.Count - 1 : colorNames.Count;

int nbHue = 6;
int nbCycle = (int)Math.Ceiling((double)size / nbHue);

var saturationMax = nbCycle <= 2 ? 1.0 : 1.0;
var saturationMin = 0.3;
var luminanceMax = nbCycle <= 2 ? 0.85 : 0.85;
var luminanceMin = 0.3;
var maxSaturationShift = 0.30;
var maxLuminanceShift = 0.15;

var interval = 1.0 / Math.Min(size, nbHue);

var saturationShift = (saturationMax - saturationMin) / (nbCycle - 1);
saturationShift = Math.Min(saturationShift, maxSaturationShift);
var luminanceShift = (luminanceMax - luminanceMin) / (nbCycle - 1);
luminanceShift = Math.Min(luminanceShift, maxLuminanceShift);

var hueShift = 0.0;

var saturation = saturationMax;
var luminance = luminanceMax;
for(var i = 0; i<size; i++)
{
if(i > 0 && (i % nbHue == 0)) // Next Cycle
{
saturation -= saturationShift;
luminance -= luminanceShift;
hueShift = hueShift == 0 ? interval/2 : 0;
}
var hue = interval*(i%nbHue) + hueShift;

System.Drawing.Color color = HSL2RGB(hue, saturation, luminance);

_colorSet.AddOrUpdate(prefix + colorNames[i], color, ???);
}
if(forceLastToGray)
{
_colorSet.TryAdd(prefix + colorNames[colorNames.Count - 1], System.Drawing.Color.LightGray);
}

_cssDirty = true;
}

如果颜色存在新值,我希望能够更新字典。如果字典中没有颜色,也添加到字典中。
我正在使用 AddOrUpdate 但无法获取 AddOrUpdate 方法的第三个参数(形成 lambda 表达式或委托(delegate)方法)。
知道我的第三个参数会是什么样子吗?

最佳答案

来自documentation :

updateValueFactory Type: System.Func The function used to generate a new value for an existing key based on the key's existing value

如果值已经存在,这将单独保留集合中的值:

_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
return existingVal;
});

这会将集合中的值替换为为插入指定的相同值:

_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
return color;
});

例如,您可以执行条件逻辑、旧值和新值之间的比较,或更新函数中的原始对象。

_colorSet.AddOrUpdate(prefix + colorNames[i], color,
(key, existingVal) =>
{
if (existingVal.Name == "Red")
return existingVal;
else
return color;
});

关于c# - 并发字典 AddOrUpdate 方法第三个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10729300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com