What is the difference between the Dictionary.Add method and the indexer Dictionary[key] = value?
What is the difference between the Dictionary.Add method and the indexer Dictionary[key] = value
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Add -> Adds an item to the dictionary if item already exists in the dictionary an exception will be thrown.
Indexer or
Dictionary[Key]=> Add Or Update. If the key doesn’t exist in the dictionary, a new item will be added. If the key exists then the value will be updated with the new value.dictionary.addwill add a new item to the dictionary,dictionary[key]=valuewill set a value to an existing entry in the dictionary against a key. If the key is not present then it (indexer) will add the item in the dictionary.In the above example, in first place
dict["OtherKey"] = "Value2";will add a new value in the dictionary because it doesn’t exist, and in second place it will modify the value to New Value.