Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Exception "Item with Same Key has already been added"

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

like image 966
Jason W Avatar asked Oct 22 '14 20:10

Jason W


People also ask

What does an item with the same key has already been added mean?

Your item with the same key has already been added is also an utra general error, but typically it can be trying to set the same key in a database (or forgetting to set a key with no default value, hence for instance if an int, then all rows would be 0, de default) or a dictionary.

Can a dictionary have duplicate keys C#?

In Dictionary, the key cannot be null, but value can be. In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.


1 Answers

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.

like image 67
Jdinklage Morgoone Avatar answered Oct 15 '22 11:10

Jdinklage Morgoone