ref: 迁移 ToDictionary、ConcurrentList

This commit is contained in:
龙腾猫跃 committed 2026-04-27 22:00:22 +08:00
1 parent c39856f2d6
commit 608ab6fd7f
3 files changed
+46 -3

No files matched your search

+22 -3
View File
@@ -1,6 +1,4 @@
using System.Collections.Concurrent;
namespace MeloongCore.Extensions;
namespace MeloongCore.Extensions;
public static class EnumerableExtensions {
#region Distinct
@@ -159,6 +157,27 @@ public static class EnumerableExtensions {
#endregion
#region Dictionary
/// <summary>
/// 从 <see cref="ConcurrentDictionary{TKey, TValue}"/> 中移除具有指定键的元素。
/// 返回是否确实移除了元素。
/// </summary>
public static bool Remove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key) => dict.TryRemove(key, out _);
/// <summary>
/// 将二维数组转换为字典。
/// </summary>
public static Dictionary<T, T> ToDictionary<T>(this T[,] arr) where T : notnull {
Dictionary<T, T> result = [];
if (arr.Length == 0) return result;
if (arr.GetLength(1) != 2) throw new ArgumentException("数组必须为两列,第一列为 Key,第二列为 Value。");
for (int i = 0; i < arr.GetLength(0); i++) result[arr[i, 0]] = arr[i, 1];
return result;
}
#endregion
/// <summary>
/// 判断集合是否有且仅有一个元素。
/// 在输入 null 时返回 false。