C# Generic Method that takes Array of Generic Dictionaries with same type of Key, but different type

Hi there,

I have few Dictionaries which have same type of key (int) while there values type is different.

So I want to store those dictionaries in an array, and simpley remove their enteries by the keys (with touching anything related to their values).

Is this possible? If so , please advise how

Thanks

Something like this should work:

Dictionary<int, int> a = new Dictionary<int, int>();
Dictionary<int, float> b = new Dictionary<int, float>();

IDictionary[] dicts = new IDictionary[2];

dicts[0] = a;
dicts[1] = b;

foreach (var d in dicts) {
    d.Remove(1);
}
1 Like

Yes, but ideally something like that

Dictionary<int, int> a = new Dictionary<int, int>();
Dictionary<int, float> b = new Dictionary<int, float>();

IDictionary<int, _IGNORE_>[] dicts = new IDictionary<int, __IGNORE>()[2];

dicts[0] = a;
dicts[1] = b;

foreach (var d in dicts) {
    d.Remove(1);
}

Another note, im using NativeHashMap<int, IgnoreAbleValue>

C# doesn’t have wildcard generics so I don’t think this is really possible. Dictionary<int, int> and Dictionary<int, float> are entirely different types in the eyes of the compiler and the runtime. The ideal way to do this would be to have a base type of NativeHashMap that doesn’t specify a second generic type constraint which NativeHashMap<K, V> extends from (as I’ve shown above with IDictionary), but obviously you can’t rewrite Unity’s NativeHashMap.

1 Like

Aah, i could have saved a lot of time if this was a possiblity.