Handling “An Item with the same key has been added” Exception in C#

“An Item with the same key has already been added” is quite a common exception while working with Dictionary object in C#. It arises when you attempt to add a same key to the dictionary object that has a same key. In those cases, you can either update the value corresponding to the key. This can be done as given in the below screenshot.

Same Key Exception

If you do not want to update your existing Key-Value pairs, you can create a new instance to the Dictionary object and proceed with adding the Key-value pair of your choice. This will also prevent the exception.Please find the screenshot below for more details.

 

Happy coding folks!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s