103

What is the difference between the Dictionary.Add method and the indexer Dictionary[key] = value?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
rsg
  • 1,099
  • 2
  • 7
  • 6
  • I came looking because I was concerned that older versions of C# might require the use of .Add() to add elements but apparently not. – AnthonyVO May 23 '19 at 18:04

5 Answers5

178

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.add will add a new item to the dictionary, dictionary[key]=value will 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.

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Test", "Value1");
dict["OtherKey"] = "Value2"; //Adds a new element in dictionary 
Console.Write(dict["OtherKey"]);
dict["OtherKey"] = "New Value"; // Modify the value of existing element to new value
Console.Write(dict["OtherKey"]);

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.

Paul Oliver
  • 7,531
  • 5
  • 31
  • 34
Habib
  • 219,104
  • 29
  • 407
  • 436
  • And when the key does not exist in `dictionar[key]=value` ? – H H Jul 19 '12 at 09:07
  • @HenkHolterman, it will add in the dictionary with the new key – Habib Jul 19 '12 at 09:08
  • Thaks Habib. but we can add new key with dictionar[newkey]=value. Which one is betten way? – rsg Jul 19 '12 at 09:09
  • 2
    @rsg, for adding a new item, dictionary.add is much better choice, because it will let you know if the key already exists in the dictionary, Using dictionary[key], will update it if the key exists, otherwise it will add a new one – Habib Jul 19 '12 at 09:14
  • An ArgumentException will be thrown if the key already exists when using the Add method. – sauv0168 Jan 29 '15 at 14:52
36

Dictionary.Add throws an exception if the key is already present. [] when used for setting an item doesn't (it does if you try to access it for read).

x.Add(key, value); // will throw if key already exists or key is null
x[key] = value; // will throw only if key is null
var y = x[key]; // will throw if key doesn't exists or key is null
xanatos
  • 109,618
  • 12
  • 197
  • 280
18

The documentation for Add makes this very clear, I feel:

You can also use the Item property to add new elements by setting the value of a key that does not exist in the Dictionary(Of TKey, TValue); for example, myCollection[myKey] = myValue (in Visual Basic, myCollection(myKey) = myValue). However, if the specified key already exists in the Dictionary(Of TKey, TValue), setting the Item property overwrites the old value. In contrast, the Add method throws an exception if a value with the specified key already exists.

(Note that the Item property corresponds to the indexer.)

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

The behavior is identical when the key does not exist in the dictionary: the item will be added in both cases.

The behavior differs when the key already exists. dictionary[key] = value will update the value mapped to the key, while dictionary.Add(key, value) will instead throw an ArgumentException.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
-1

dictionary.add add an item to the dictionary whereas dictionary[key]=value assigns a value to an already existing key.

Jeeva
  • 4,585
  • 2
  • 32
  • 56