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

时间:2018-04-16 16:47:12

标签: c# dictionary generics

是否可以拥有一个接收通用字典参数并从中返回随机密钥的函数?由于字典“key”值可以是任何数据类型,我希望字典param是通用的,并且无论什么数据类型都返回它的随机密钥。我到目前为止写了这个,但是收到了错误。

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)];
}

我收到错误:

  

CS1503参数1:无法从'System.Collections.Generic.Dictionary<int, string>'转换为'System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<int, string>, System.Collections.Generic.Dictionary<int, string>>'

我知道如何获取Access random item in list,但我不知道如何正确地将字典传递给我的方法。

3 个答案:

答案 0 :(得分:4)

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

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    //snip
}

答案 1 :(得分:1)

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

static void Main(string[] args)
{

    using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1)) 
    {
        var sendRequest = new SendEmailRequest
        {
            Source = senderAddress,
            Destination = new Destination
            {
                ToAddresses =
                new List<string> { receiverAddress }
            },
            Message = new Message
            {
                Subject = new Content(subject),
                Body = new Body
                {
                    Html = new Content
                    {
                        Charset = "UTF-8",
                        Data = htmlBody
                    },
                    Text = new Content
                    {
                        Charset = "UTF-8",
                        Data = textBody
                    }
                }
            },

            ConfigurationSetName = configSet
        };
        try
        {
            Console.WriteLine("Sending email using Amazon SES...");
            var response = client.SendEmailAsync(sendRequest).GetAwaiter().GetResult();
            Console.WriteLine("The email was sent successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("The email was not sent.");
            Console.WriteLine("Error message: " + ex.Message);

        }
    }

    Console.Write("Press any key to continue...");
    Console.ReadKey();
}

并使用它:

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)];
}

答案 2 :(得分:0)

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

int randomKey = (int)RandomDictionaryKeyValue<int, string>(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)];
}