Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of Dictionary.Add vs Dictionary[key]=value [duplicate]

Tags:

c#

.net

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

like image 329
rsg Avatar asked Jul 19 '12 09:07

rsg


People also ask

Can a dictionary have duplicate keys C?

In Dictionary, the key cannot be null, but value can be. In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.

What is the default value of dictionary C#?

Dictionary<string, int> TotalMarks = new Dictionary<string, int>(); TotalMarks. DefaultValue = 0; Console.

How do you check if a key is in a dictionary C#?

Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false.

Is dictionary unique c#?

A dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory because for each value there is also a key.


4 Answers

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.

like image 197
Habib Avatar answered Sep 28 '22 17:09

Habib


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
like image 28
xanatos Avatar answered Sep 28 '22 17:09

xanatos


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.)

like image 28
Jon Skeet Avatar answered Sep 28 '22 18:09

Jon Skeet


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.

like image 40
cdhowie Avatar answered Sep 28 '22 19:09

cdhowie