Thursday, September 13, 2012

Item has already been added. Key in dictionary: 'key' Key being added: 'key'

If you try to add same key to a hashtable you may encounter this error

var ht = new Hashtable();
ht.Add("key", "Data1");
.
.
.
ht.Add("key", "Data2");

To avoid this use you can simply change this code like


var ht = new Hashtable();
ht["key"] = "Data1";
.
.
.
ht["key"] = "Data2";

This index will add the key, if it is not in the hashtable, else will just update the value

No comments:

Post a Comment