Parallel Dictionary AddOrUpdate Method 3rd Parameter?

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;
}

I want to be able to update the dictionary if the color exists with a new value. Also add to the dictionary if there is no color in the dictionary.
I use AddOrUpdate, but I can not get the third parameter (generate a lambda expression or delegation method) of the AddOrUpdate method.
Any idea what my third parameter would look like?

+5
source share
3 answers

From the documentation :

updateValueFactory Type: System.Func Function used to generate a new value for an existing key based on a key existing value

This will leave the value only in the collection if it already exists:

_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;
            });
+16

-, ,

Func<TKey, TValue, TValue>

, , , . , , .

(key, oldValue) => oldValue

, . ,

_colorSet.AddOrUpdate(prefix + colorNames[i], color, (key, oldValue) => color); 
+1

, , ; . , .

_colorSet[prefix + colorNames[i]] = color;
0

All Articles