Admin.Core/src/platform/ZhonTai.Common/Extensions/DictionaryExtension.cs
(完整代码) 1KBusing System;
using System.Collections.Generic;
namespace ZhonTai.Common.Extensions;
public static class DictionaryExtension
{
public static TValue GetOrAdd<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
Func<TKey, TValue> factory)
{
TValue obj;
if (dictionary.TryGetValue(key, out obj))
{
return obj;
}
return dictionary[key] = factory(key);
}
public static TValue GetOrAdd<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
Func<TValue> factory)
{
return dictionary.GetOrAdd(key, k => factory());
}
public static TValue AddOrUpdate<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
Func<TKey, TValue> addFactory,
Func<TKey, TValue, TValue> updateFactory)
{
TValue obj;
if (dictionary.TryGetValue(key, out obj))
{
obj = updateFactory(key, obj);
}
else
{
obj = addFactory(key);
}
dictionary[key] = obj;
return obj;
}
public static TValue AddOrUpdate<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
Func<TValue> addFactory,
Func<TValue, TValue> updateFactory)
{
return dictionary.AddOrUpdate(key, k => addFactory(), (k, v) => updateFactory(v));
}
}
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
