浏览代码

优化字典扩展

懒得勤快 2 年之前
父节点
当前提交
2701d84ad3
共有 1 个文件被更改,包括 6 次插入5 次删除
  1. 6 5
      Masuit.Tools.Abstractions/Extensions/BaseType/IDictionaryExtensions.cs

+ 6 - 5
Masuit.Tools.Abstractions/Extensions/BaseType/IDictionaryExtensions.cs

@@ -660,11 +660,11 @@ namespace Masuit.Tools
         /// <param name="key"></param>
         /// <param name="addValueFactory"></param>
         /// <returns></returns>
-        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TValue> addValueFactory)
+        public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, Func<TValue> addValueFactory)
         {
             if ([email protected](key))
             {
-                @this.Add(key, addValueFactory());
+                @this[key] = addValueFactory();
             }
 
             return @this[key];
@@ -683,7 +683,7 @@ namespace Masuit.Tools
         {
             if ([email protected](key))
             {
-                @this.Add(key, await addValueFactory());
+                @this[key] = await addValueFactory();
             }
 
             return @this[key];
@@ -698,11 +698,12 @@ namespace Masuit.Tools
         /// <param name="key"></param>
         /// <param name="addValue"></param>
         /// <returns></returns>
-        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue)
+        public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
         {
             if ([email protected](key))
             {
-                @this.Add(key, addValue);
+                @this[key] = addValue;
+                return addValue;
             }
 
             return @this[key];