参数异常“具有相同密钥的项目已被添加” [英] Argument Exception "Item with Same Key has already been added"
问题描述
我继续使用以下代码发生错误:
字典< string,string> rct3Features = new Dictionary< string,string>();
字典< string,string> rct4Features = new Dictionary< string,string>();
foreach(rct3Lines中的字符串)
{
string [] items = line.Split(new String [] {},2,StringSplitOptions.None)
rct3Features.Add(items [0],items [1]);
////打印出字典(查看是否可以使用)
// foreach(rct3Features中的KeyValuePair< string,string>项目)
// {
// Console.WriteLine(item.Key ++ item.Value);
//}
}
错误会抛出一个 ArgumentException
说,
已经添加了具有相同密钥的项目。
我不确定几次Google搜索如何解决这个问题。
稍后在代码中,我需要访问比较函数的字典:
Compare4To3(rct4Features,rct3Features);
public static void Compare4To3(Dictionary< string,string> dictionaryOne,Dictionary< string,string> dictionaryTwo)
{
// foreach(dictionaryOne中的字符串项)
// {
//打印出字典(查看是否有效)
foreach(KeyValuePair< dictionaryOne中的string>项目)
{
Console.WriteLine(item.Key ++ item.Value);
}
// if(dictionaryTwo.ContainsKey(dictionaryOne.Keys)
// {
// Console.Write(True);
//}
// else
// {
// Console.Write(False);
//}
//}
}
此函数未完成,但我正在尝试解决此异常。方法我可以解决这个异常错误,并保持访问该字典与此功能一起使用?谢谢
此错误是字典键是唯一的,你不能有多个相同的键。要解决这个问题,你应该修改你的代码,如下所示:
字典< string,string> rct3Features = new Dictionary< string,string>();
字典< string,string> rct4Features = new Dictionary< string,string>();
foreach(rct3Lines中的字符串)
{
s tring [] items = line.Split(new String [] {},2,StringSplitOptions.None);
如果(!rct3Features.ContainsKey(items [0]))
{
rct3Features.Add(items [0],items [1]);
}
////打印出字典(查看是否有效)
// foreach(rct3Features中的KeyValuePair< string,string>项)
// {
// Console.WriteLine(item.Key ++ item.Value);
//}
}
这个简单的 if
语句确保只有当Key( items [0]
))尚未存在时,才尝试向Dictionary添加新条目。 / p>
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);
//}
}
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);
//}
}
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.
这篇关于参数异常“具有相同密钥的项目已被添加”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!