zoukankan      html  css  js  c++  java
  • Dictionary GetOrAdd

    public static TValue GetOrAdd<TKey,TValue>(
       this Dictionary<TKey,TValue> dictionary,
       TKey key,
       Func<TKey, TValue> valueFactory)
    {
       TValue value;
       if (!dictionary.TryGetValue(key, out value))
       {
           value = valueFactory(key);
           dictionary.Add(key, value);
       }
       return value;
    }
    
    public static TValue AddOrUpdate<TKey,TValue>(
       this Dictionary<TKey,TValue> dictionary,
       TKey key,
       Func<TKey, TValue> addValueFactory,
       Func<TKey, TValue, TValue> updateValueFactory)
    {
       TValue value;
       if (dictionary.TryGetValue(key, out value))
       {
           value = updateValueFactory(key, value);
       }
       else
       {
           value = addValueFactory(key);
       }
       dictionary[key] = value;
       return value;
    }
  • 相关阅读:
    博客园的界面设置
    ARM 汇编指令集
    winfroms更换皮肤
    面向对象的七项设计原则
    S2-01
    机票查询与订购系统
    重点语法
    第二章
    一、17.09.13
    习作
  • 原文地址:https://www.cnblogs.com/zeroone/p/8043306.html
Copyright © 2011-2022 走看看