参数异常"具有相同的密钥项已添加" [英] Argument Exception "Item with Same Key has already been added"

查看:513
本文介绍了参数异常"具有相同的密钥项已添加"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到错误用下面的代码:

I keep getting an error with the following code:

        Dictionary<string, string> rct3Features = new Dictionary<string, string>();
        Dictionary<string, string> rct4Features = new Dictionary<string, string>();

        foreach (string line in rct3Lines) 
        {
            string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);

            rct3Features.Add(items[0], items[1]);

            ////To print out the dictionary (to see if it works)
            //foreach (KeyValuePair<string, string> item in rct3Features)
            //{
            //    Console.WriteLine(item.Key + " " + item.Value);
            //}

        }



错误引发ArgumentException他说:具有相同键的项已被添加。 。我不确定经过几次谷歌搜索如何解决这个问题。

The error throws an ArgumentException saying, "An item with the same key has already been added." I am unsure after several Google searches how to fix this.

在后面的代码我需要访问字典比较功能:

Later in the code I need to access the dictionary for a compare function:

Compare4To3(rct4Features, rct3Features);

public static void Compare4To3(Dictionary<string, string> dictionaryOne, Dictionary<string, string> dictionaryTwo)
    {
        //foreach (string item in dictionaryOne)
        //{

        //To print out the dictionary (to see if it works)
        foreach (KeyValuePair<string, string> item in dictionaryOne)
        {
            Console.WriteLine(item.Key + " " + item.Value);
        }

            //if (dictionaryTwo.ContainsKey(dictionaryOne.Keys)
            //{
            //    Console.Write("True");
            //}
            //else
            //{
            //    Console.Write("False");
            //}
        //}
    }

这功能还没有完成,但我试图解决这个异常。什么方法我可以解决此异常错误,并保持访问字典使用此功能的使用吗?谢谢

This function isn't completed, but I am trying to resolve this exception. What are the ways I can fix this exception error, and keep access to the dictionary for use with this function? Thank you

推荐答案

此错误。相当不言自明字典键是独一无二的,你不能有相同的键的多个要解决这个问题,您应该修改,像这样的代码:

This error is fairly self-explanatory. Dictionary keys are unique and you cannot have more than one of the same key. To fix this, you should modify your code like so:

    Dictionary<string, string> rct3Features = new Dictionary<string, string>();
    Dictionary<string, string> rct4Features = new Dictionary<string, string>();

    foreach (string line in rct3Lines) 
    {
        string[] items = line.Split(new String[] { " " }, 2, StringSplitOptions.None);

        if (!rct3Features.ContainsKey(items[0]))
        {
            rct3Features.Add(items[0], items[1]);
        }

        ////To print out the dictionary (to see if it works)
        //foreach (KeyValuePair<string, string> item in rct3Features)
        //{
        //    Console.WriteLine(item.Key + " " + item.Value);
        //}

    }

这简单的如果语句确保你只尝试添加一个新条目解释时,密钥(项目[0] )不已经存在了。

This simple if statement ensures that you are only attempting to add a new entry to the Dictionary when the Key (items[0]) is not already present.

这篇关于参数异常&QUOT;具有相同的密钥项已添加&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆