I get a runtime error:
An item with the same key has already been added.
private class ProductUpdate
{
public int Index = 0;
public string ProductCode;
}
private void UpdateProduct( List
{
Dictionary
for ( int i = 0; i < products.Count; i++ )
{
productUpdateDictionary.Add( products[i].code, new ProductUpdate { Index = i, ProductCode = products[i].code } );
}
}
I don't know how to solve this issue ,any body has any suggestion
Thanks a Lot!

VulpesRank #55 · 29.2K reputationPosted Mar 17, 2015, 2:00 PM EDT
You can avoid the error by checking first whether the key already exists, like this:
However, this may not be entirely satisfactory and a different approach would be to modify products[i].code in some way so that it differs from the previous key and can therefore be added to the Dictionary.
kris26 reputationPosted Mar 18, 2015, 1:20 AM EDT