通用字典参数c#(从中返回随机键)

通用字典参数c#(从中返回随机键),c#,dictionary,generics,C#,Dictionary,Generics,是否可能有一个函数接收泛型字典参数并从中返回一个随机键?因为dictionary“key”值可以是任何数据类型,所以我希望dictionary参数是泛型的,并从中返回一个随机键,而不管数据类型是什么。到目前为止,我写了这篇文章,但有一个错误 Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(1, "1003206"); dict.Add(2, "1234567"); dict.Ad

是否可能有一个函数接收泛型字典参数并从中返回一个随机键?因为dictionary“key”值可以是任何数据类型,所以我希望dictionary参数是泛型的,并从中返回一个随机键,而不管数据类型是什么。到目前为止,我写了这篇文章,但有一个错误

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)];
}
Dictionary dict=new Dictionary();
添加(1,“1003206”);
添加(2,“1234567”);
添加(3,“5432567”);
int randomKey=(int)RandomDictionaryKeyValue(dict);
私有T随机字典Y值(字典dict)
{
列表键列表=新列表(dict.Keys);
Random rand=新的Random();
返回keyList[rand.Next(keyList.Count)];
}
我得到一个错误:

CS1503参数1:无法从
'System.Collections.Generic.Dictionary'
转换为
'System.Collections.Generic.Dictionary'


我知道如何获取,但我不知道如何正确地将dictionary传递给我的方法。

问题是您为dictionary的键和值指定了相同的泛型类型

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    //snip
}
private-TKey-randoomDictionaryKeyValue(字典dict-dict)
{
//剪断
}

问题在于您为字典的键和值指定了相同的泛型类型

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    //snip
}
private-TKey-randoomDictionaryKeyValue(字典dict-dict)
{
//剪断
}

如果只想从字典中获取随机键,只需将键类型和值类型传递给方法,而不是整个字典类型:

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    List<TKey> keyList = new List<TKey>(dict.Keys);

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}
private-TKey-randoomDictionaryKeyValue(字典dict-dict)
{
列表键列表=新列表(dict.Keys);
Random rand=新的Random();
返回keyList[rand.Next(keyList.Count)];
}
然后像这样使用它:

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = RandomDictionaryKeyValue<int, string>(dict);
Dictionary dict=new Dictionary();
添加(1,“1003206”);
添加(2,“1234567”);
添加(3,“5432567”);
int randomKey=随机值(dict);

如果只想从字典中获取随机键,只需将键类型和值类型传递给方法,而不是整个字典类型:

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    List<TKey> keyList = new List<TKey>(dict.Keys);

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}
private-TKey-randoomDictionaryKeyValue(字典dict-dict)
{
列表键列表=新列表(dict.Keys);
Random rand=新的Random();
返回keyList[rand.Next(keyList.Count)];
}
然后像这样使用它:

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = RandomDictionaryKeyValue<int, string>(dict);
Dictionary dict=new Dictionary();
添加(1,“1003206”);
添加(2,“1234567”);
添加(3,“5432567”);
int randomKey=随机值(dict);
字典dict=new Dictionary();
添加(1,“1003206”);
添加(2,“1234567”);
添加(3,“5432567”);
int randomKey=(int)RandomDictionaryKeyValue(dict);
在我回来检查之前,我找到了答案

private object RandomDictionaryKeyValue<T1, T2>(object dict)
{
    var dict2 = (Dictionary<T1, T2>)dict;
    List<T1> keyList = new List<T1>(dict2.Keys);                                                                                                                        

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}
private object randoomDictionaryKeyValue(object dict)
{
var dict2=(字典)dict;
列表键列表=新列表(dict2.Keys);
Random rand=新的Random();
返回keyList[rand.Next(keyList.Count)];
}
字典dict=new Dictionary();
添加(1,“1003206”);
添加(2,“1234567”);
添加(3,“5432567”);
int randomKey=(int)RandomDictionaryKeyValue(dict);
在我回来检查之前,我找到了答案

private object RandomDictionaryKeyValue<T1, T2>(object dict)
{
    var dict2 = (Dictionary<T1, T2>)dict;
    List<T1> keyList = new List<T1>(dict2.Keys);                                                                                                                        

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}
private object randoomDictionaryKeyValue(object dict)
{
var dict2=(字典)dict;
列表键列表=新列表(dict2.Keys);
Random rand=新的Random();
返回keyList[rand.Next(keyList.Count)];
}

很明显,您不能像本文所示创建
Random
,但每个人都知道-所以请注意,示例显示了如何将dictionary作为参数传递,而不是如何从列表中获取Random元素-Good spot@AlexeiLevenkov,我已经完全删除了内容。显然,你不能像本文所示创建
Random
,但每个人都知道-所以请注意,示例显示了如何将dictionary作为参数传递,而不是如何从列表中获取Random元素-Good spot@AlexeiLevenkov,我已经完全删除了内容。甚至示例用法,甚至连示例用法都不会编译。