I know that I can define a dictionary with a System.ValueTuple key (based on this answer) as below:
var multiKeyDictionary = new Dictionary<(int key1, string key2), object>()
{
{(1, "test"), new object() }
};
However, I want any string
values present in the ValueTuple
to be treated as case-insensitive using IEqualityComparer
or something similarly idiomatic for dictionary access, similar to what is prescribed here.
The solutions I have thought of so far are to:
- Implement IEqualityComparer like was necessary for
System.Tuple
- Wrap all access to the dictionary as below:
class InsensitiveValueTupleDictionary
{
private Dictionary<(int key1, string key2), object> _multiKeyDictionary = new Dictionary<(int key1, string key2), object>();
public void Add((int key1, string key2) key, object value)
{
_multiKeyDictionary.Add((key.key1, key.key2.ToLower()), value);
}
public bool ContainsKey((int key1, string key2) key)
{
return _multiKeyDictionary.ContainsKey((key.key1, key.key2.ToLower()));
}
// ... and so on
}
Both of these strategies will require setup code for every unique combination of ValueTuple
e.g., (int, string)
, (string, int)
, (string, string, int)
, and so on.
Is there another method that will allow case-insensitive key equality for arbitrary ValueTuple
combinations?