A generic implementation of a dictionary that maps keys to pairs of values, where each key is associated with two distinct values.
MultiDictionary<TKey, TValue1, TValue2>
is a strongly-typed collection that provides similar functionality to Dictionary<TKey, TValue>
but associates each key with a tuple of two values instead of a single value.
Key features:
- Associates each key with a pair of values
- Standard dictionary operations (Add, Remove, ContainsKey, etc.) with value pairs
- Thread-unsafe (use external synchronization)
- Similar performance characteristics to standard dictionary
- Implements
IDictionary<TKey, (TValue1, TValue2)>
NuGet package may be coming soon. Until then, you can clone this repository or download the source code directly.
var dict = new MultiDictionary<string, int, DateTime>();
// Add items
dict.Add("key1", 42, DateTime.Now);
dict.AddOrUpdate("key2", 99, DateTime.Today);
// Get values
if (dict.TryGetValue("key1", out var value1, out var value2))
{
Console.WriteLine($"Value1: {value1}, Value2: {value2}");
}
// Update values
dict["key1"] = (100, DateTime.MinValue);
dict.TryUpdate("key2", 200, DateTime.MaxValue);
// Remove items
dict.Remove("key1");
// With custom capacity (initial size)
var dict1 = new MultiDictionary<string, double, bool>(capacity: 100);
// With custom key comparer (case-insensitive keys)
var dict2 = new MultiDictionary<string, int, int>(
comparer: StringComparer.OrdinalIgnoreCase);
// With both capacity and comparer
var dict3 = new MultiDictionary<string, int, int>(
capacity: 50,
comparer: StringComparer.OrdinalIgnoreCase);
foreach (var kvp in dict)
{
Console.WriteLine($"Key: {kvp.Key}, Value1: {kvp.Value.Item1}, Value2: {kvp.Value.Item2}");
}
Count
- Gets the number of key/value pairsIsReadOnly
- Indicates if dictionary is read-onlyKeys
- Collection of keysValues
- Collection of value pairsItem[TKey]
- Indexer for getting/setting values
Add(TKey, TValue1, TValue2)
- Adds key with specified valuesAddOrUpdate(TKey, TValue1, TValue2)
- Adds or updates key/value pairClear()
- Removes all itemsContainsKey(TKey)
- Checks for key existenceTryGetValue(TKey, out TValue1, out TValue2)
- Gets both valuesRemove(TKey)
- Removes key/value pairTryUpdate(TKey, TValue1, TValue2)
- Updates existing key's values
This project is licensed under the MIT License - see the LICENSE file for details.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.