Is it possible to have a function that receives a generic dictionary param and return a random key from it? Since dictionary "key" values can be any data type, I would like the dictionary param to be generic and return a random key from it no matter what data type. I wrote this so far but am getting an error.
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");
int randomKey = (int)RandomDictionaryKeyValue<Dictionary<int, string>>(dict);
private T RandomDictionaryKeyValue<T>(Dictionary<T, T> dict)
{
List<T> keyList = new List<T>(dict.Keys);
Random rand = new Random();
return keyList[rand.Next(keyList.Count)];
}
I'm getting error:
CS1503 Argument 1: cannot convert from
'System.Collections.Generic.Dictionary<int, string>'
to'System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<int, string>, System.Collections.Generic.Dictionary<int, string>>'
I know how to get Access random item in list, but I don't know how to correctly pass dictionary to my method.