C#字典,如何获取特定值

时间:2016-11-03 23:55:19

标签: c# dictionary

我创建了一个字典,我希望用户编写城市(kommune),然后在名为txtKommuneresultalt的文本框中显示值(procent)

我是C#的新手,所以我希望有人可以帮助我

我已经尝试了几天搜索,没有任何效果。我正在使用Windowsforms,并准备好buttonHandler

到目前为止,这是我的代码:

Dictionary<double, string> dictionary = new Dictionary<double, string>();
double procent = Convert.ToDouble(txtKommuneresultat.Text);
string kommune = txtKommuneInput.Text;


dictionary.Add(11.44, "Faxe");
dictionary.Add(4.29, "Greve");
dictionary.Add(7.11, "Gulborgsund");
dictionary.Add(7.86, " Holbæk");
dictionary.Add(5.67, "Kalundborg");
dictionary.Add(4.99, "Køge");
dictionary.Add(7.28, "Lejre");
dictionary.Add(2.67, "Lolland");
dictionary.Add(4.07, "Næstved");
dictionary.Add(1.21, "Odsherred");
dictionary.Add(5.02, "Ringsted");
dictionary.Add(13.23, "Slagelse");
dictionary.Add(20.75, "Solrød");
dictionary.Add(1.81, "Sorø");
dictionary.Add(5.50, "Stevns");
dictionary.Add(1.29, "Vordingborg");

txtKommuneresultat.Show();

3 个答案:

答案 0 :(得分:3)

dictionary中的第一个类型是,第二个类型是。字典允许您根据该值查找

目前,您有double作为第一种类型(键)和string作为第二种类型(值)。这样,您就可以使用string值作为键来查找double值。

但是等等。您的双打代表一个百分比,而您的字符串代表一个城市名称。您想根据城市名称查找百分比。因此string应该是词典中的,而不是双倍。

Dictionary<string, double> dictionary = new Dictionary<string, double>();

这允许您通过提供正确的字符串来获取特定的double值:

var percentage = dictionary["Gulborgsund"];

由于查找将基于用户输入(通常不可靠),因此最好使用TryGetValue方法:

double percentage;
if (dictionary.TryGetValue(userInput, out percentage)) 
{
    // Use percentage
}
else 
{
    // Could not retrieve value
}

答案 1 :(得分:0)

        string userInput = txtKommuneInput.Text;
        double procent;
        if (dictionary.TryGetValue(userInput, out procent))

            txtKommuneresultat.Text = procent.ToString();

答案 2 :(得分:-2)

如何捕鱼:

这是在C#中学习字典的良好开端:

https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx#Anchor_8

另外,MSDN有很多有用的资源,请随时浏览。

如何做(我认为这将特定于您的情况,我分为两个选项,使用城市名称作为键或使用百分比作为键):

using System;
using System.Web.SessionState;
using System.Collections.Generic;

public class Program
{
    public static void Main()       
    {
        //Part 1: Using percentage as Key
        Dictionary<double, string> dictionary = new Dictionary<double, string>();

        dictionary.Add(11.44, "Faxe");          
        //(...)         
        dictionary.Add(1.29, "Vordingborg");

        //Here is Komune
        dictionary.Add(5.89, "Komune");

        if (dictionary.ContainsKey(5.89)){
            Console.WriteLine(String.Format("Found: {0}!", dictionary[5.89]));
        } else {
            Console.WriteLine("Not Found!");
        }



        //Part 2: Using the string as Key
        Dictionary<string,double> dictionaryStringPercent = new Dictionary<string,double>();

        dictionaryStringPercent.Add("Faxe",11.44);                  
        //(...)         
        dictionaryStringPercent.Add("Vordingborg",1.0);

        //Here is Komune
        dictionaryStringPercent.Add("Komune",5.89);

        if (dictionaryStringPercent.ContainsKey("Komune")){
            Console.WriteLine(String.Format("Found: {0}!", dictionaryStringPercent["Komune"]));
        } else {
            Console.WriteLine("Not Found!");
        }
    }
}