Files
SabreTools.IO/SabreTools.Collections.Extensions/DictionaryExtensions.cs
Matt Nadareski 77dc8ebed7 Separate out collections extensions
Even though there are relatively few collections extensions right now, there is a relatively high chance that other extension will be used in the future.
2026-03-23 10:50:09 -04:00

34 lines
1.1 KiB
C#

using System.Collections.Generic;
namespace SabreTools.Collections.Extensions
{
public static class DictionaryExtensions
{
/// <summary>
/// Merge a dictionary into an existing one, if possible
/// </summary>
/// <param name="dict">Source dictionary to add to</param>
/// <param name="other">Second dictionary to add from</param>
/// <remarks>This only performs a shallow copy</remarks>
public static void MergeWith(this Dictionary<string, List<string>> dict, Dictionary<string, List<string>> other)
{
// Ignore if there are no values to append
if (other.Count == 0)
return;
// Loop through and add from the new dictionary
foreach (var kvp in other)
{
// Ignore empty values
if (kvp.Value.Count == 0)
continue;
if (!dict.ContainsKey(kvp.Key))
dict[kvp.Key] = [];
dict[kvp.Key].AddRange(kvp.Value);
}
}
}
}