Skip to content

Commit 96f18f7

Browse files
committed
feat(dictionary-helper): add utility methods
1 parent 849b4f4 commit 96f18f7

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

SharpHelpers/SharpHelpers/DictionaryHelper.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,76 @@ public static string ToReadableString<TKey, TValue>(this Dictionary<TKey, TValue
120120
entries.Add($"{kvp.Key}: {kvp.Value}");
121121
}
122122
return "{" + string.Join(", ", entries) + "}";
123+
}
124+
125+
/// <summary>
126+
/// Returns the value associated with the specified key, or the default value if the key does not exist.
127+
/// </summary>
128+
/// <param name="dictionary">The dictionary to query.</param>
129+
/// <param name="key">The key to look for.</param>
130+
/// <returns>The value associated with the key, or default if not found.</returns>
131+
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
132+
{
133+
return dictionary.TryGetValue(key, out var value) ? value : default;
134+
}
135+
136+
/// <summary>
137+
/// Checks whether all specified keys exist in the dictionary.
138+
/// </summary>
139+
/// <param name="dictionary">The dictionary to check.</param>
140+
/// <param name="keys">The keys to check for existence.</param>
141+
/// <returns>True if all keys exist; otherwise, false.</returns>
142+
public static bool ContainsAllKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
143+
{
144+
return keys.All(k => dictionary.ContainsKey(k));
145+
}
146+
147+
/// <summary>
148+
/// Tries to get the value associated with the key and cast it to the specified type.
149+
/// </summary>
150+
/// <typeparam name="TKey">The key type.</typeparam>
151+
/// <typeparam name="TValue">The stored value type.</typeparam>
152+
/// <typeparam name="TResult">The desired result type.</typeparam>
153+
/// <param name="dictionary">The dictionary to query.</param>
154+
/// <param name="key">The key to retrieve.</param>
155+
/// <param name="result">The casted result if successful; otherwise, default.</param>
156+
/// <returns>True if the cast was successful; otherwise, false.</returns>
157+
public static bool TryGetValueAs<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, out TResult result)
158+
{
159+
result = default;
160+
if (dictionary.TryGetValue(key, out var value) && value is TResult casted)
161+
{
162+
result = casted;
163+
return true;
164+
}
165+
return false;
166+
}
167+
168+
/// <summary>
169+
/// Increments the value associated with the specified key by a given amount. If the key does not exist, it is added with the amount as its value.
170+
/// </summary>
171+
/// <param name="dictionary">The dictionary to operate on.</param>
172+
/// <param name="key">The key whose value to increment.</param>
173+
/// <param name="amount">The amount to increment by.</param>
174+
public static void IncrementValue<TKey>(this IDictionary<TKey, int> dictionary, TKey key, int amount = 1)
175+
{
176+
if (dictionary.ContainsKey(key))
177+
dictionary[key] += amount;
178+
else
179+
dictionary[key] = amount;
180+
}
181+
182+
/// <summary>
183+
/// Adds multiple key-value pairs to the dictionary. If a key already exists, it is updated with the new value.
184+
/// </summary>
185+
/// <param name="dictionary">The dictionary to update.</param>
186+
/// <param name="items">The key-value pairs to add or update.</param>
187+
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> items)
188+
{
189+
foreach (var kvp in items)
190+
{
191+
dictionary[kvp.Key] = kvp.Value;
192+
}
123193
}
124194
}
125195
}

0 commit comments

Comments
 (0)