/// <summary> /// 提供一个方法遍历所有key值 /// </summary> public static void ForeachKey <TKey, TValue>(this Dictionary <TKey, TValue> dic, CallBackWithParamsReturn <TKey, bool> action) { if (action == null) { return; } var enumerator = dic.GetEnumerator(); while (enumerator.MoveNext()) { if (!action(enumerator.Current.Key)) { break; } } }
/// <summary> /// 提供一个方法遍历所有项 /// </summary> public static void Foreach <TKey, TValue>(this Dictionary <TKey, TValue> dic, CallBackWithParamsReturn <TKey, TValue, bool> action, int maxCount = 1000) { if (action == null) { return; } var enumerator = dic.GetEnumerator(); int i = 0; while (enumerator.MoveNext() && i++ < maxCount) { if (!action(enumerator.Current.Key, enumerator.Current.Value)) { break; } } }