C#有一种方法可以简化嵌套函数

时间:2019-10-11 18:16:01

标签: c# dictionary

我有一个用于字典的帮助程序类以更新值。代码如下所示:

public static void UpdateWithAggregate(
    ConcurrentDictionary<string, 
        ConcurrentDictionary<string, ConcurrentDictionary<string, double>>> data,
    string key1,
    string key2,
    string key3,
    double value)
{
    if (!data.ContainsKey(key1))
    {
        data[key1] = 
            new ConcurrentDictionary<string, ConcurrentDictionary<string, double>>();
    }
    UpdateWithAggregate(data[key1], key2, key3, value);
}

public static void UpdateWithAggregate(
    ConcurrentDictionary<string, ConcurrentDictionary<string, double>> data,
    string key1,
    string key2,
    double value)
{
    if (!data.ContainsKey(key1))
    {
        data[key1] = new ConcurrentDictionary<string, double>();
    }
    UpdateWithAggregate(data[key1], key2, value);
}

public static void UpdateWithAggregate(
    ConcurrentDictionary<string, double> data,
    string key1,
    double value)
{
    if (!data.ContainsKey(key1))
    {
        data[key1] = value;
    }
    else
    {
        data[key1] += value;
    }
}

我想知道是否有一种方法可以使用单个函数并采用任意嵌套的字典及其键并进行添加。

0 个答案:

没有答案