Concurrent Dictionary AddOrUpdate method 3rd parameter?

13,850

Solution 1

From the documentation:

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

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

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

This will replace the value in the collection with the same one specified for the insert:

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

You can perform conditional logic, comparisons between the old value and new value, or update the original object in the function, for example.

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

Solution 2

As per the web page asawyer gave you, what's required is a function

Func<TKey, TValue, TValue>

In this case it looks like you are passing a string and a Color but how you want to combing them is largely upto you. You need a function that returns a Color so the following should work from a syntax perspective.

(key, oldValue) => oldValue

I've no idea who you might calculating the new value. You could for example use your new color

_colorSet.AddOrUpdate(prefix + colorNames[i], color, (key, oldValue) => color); 
Share:
13,850
Ruruboy
Author by

Ruruboy

Founder of a start up Software Consulting company providing consulting services to IT companies in North America. I like writing code in MS.NET, C#, SQL Server,etc as its embedded in my soul and is a passionate phase of my existence in this world. Software Development is the essence of existence. One does not need an interview to validate his knowledge but a computer to test his skill level. Computing is the abstract on which an entity can be evaluated.

Updated on June 25, 2022

Comments

  • Ruruboy
    Ruruboy almost 2 years
    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 new value. And also add to dictionary if the color is not there in dictionary.
    I am using the AddOrUpdate but not able to get the 3rd parameter(form the lambda expression OR delegate method) of the AddOrUpdate method.
    Any idea how my 3rd parameter would look like?