简体   繁体   English

C#中的字典

[英]Dictionaries in C#

This program is meant to read in a csv file and create a dictionary from it, which is then used to translate a word typed into a textbox (txtINPUT) and output the result to another textbox (txtOutput). 此程序用于读取csv文件并从中创建字典,然后将其用于将键入的单词转换为文本框(txtINPUT)并将结果输出到另一个文本框(txtOutput)。

The program doesnt translate anything and always outputs "No translation found." 该程序不会翻译任何内容,并始终输出“未找到翻译。” I've never used the dictionary class before so I dont know where the problem is coming from. 我以前从未使用过字典类,所以我不知道问题出在哪里。

Thanks for any help you can give me. 感谢你给与我的帮助。

    Dictionary<string, string> dictionary; 

    private void CreateDictionary()
    {
        //Load file
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader("dictionarylist.csv"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                //Add to dictionary
                dictionary = new Dictionary<string, string>();
                string[] split = line.Split(',');
                dictionary.Add(split[0], split[1]);
            }
        }
    }
        private void btnTranslate_Click(object sender, EventArgs e)
    {
        CreateDictionary();

        string outputString = null;
        if (dictionary.TryGetValue(txtInput.Text, out outputString))
        {
            txtOutput.Text = outputString; 
        }
        else
        {
            txtOutput.Text = ("No translation found");
        }

    }

You are creating a new instance of a Dictionary each loop cycle, basically overwriting it each time you read a line. 您正在为每个循环周期创建一个新的Dictionary实例,每次读取一行时基本上都会覆盖它。 Move this line out of the loop: 将此行移出循环:

// Instantiate a dictionary
var map = new Dictionary<string, string>();

Also why not load dictionary one time, you are loading it each button click, this is not efficient. 另外为什么不加载字典一次,你加载每个按钮点击,这是不高效的。

( >=.NET 3 ) The same using LINQ ToDictionary() : >=.NET 3 )使用LINQ ToDictionary()

usign System.Linq;
var map = File.ReadAllLines()
              .Select(l =>
               {
                    var pair = l.Split(',');
                    return new { First = pair[0], Second = pair[1] }
               })
              .ToDictionary(k => k.First, v => v.Second);

In your while loop, you create a new dictionary every single pass! 在while循环中, 每次传递都会创建一个新字典

You want to create one dictionary, and add all the entries to that: 您想要创建一个字典,并将所有条目添加到该字典:

while ((line = reader.ReadLine()) != null)
{
    //Add to dictionary
    dictionary = new Dictionary<string, string>();  /* DON'T CREATE NEW DICTIONARIES */
    string[] split = line.Split(',');
    dictionary.Add(split[0], split[1]);
}

You should do it more like this: 你应该这样做:

List<string> list = new List<string>();
dictionary = new Dictionary<string, string>();  /* CREATE ONE DICTIONARY */
using (StreamReader reader = new StreamReader("dictionarylist.csv"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        string[] split = line.Split(',');
        dictionary.Add(split[0], split[1]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM