Khalil Khalaf
Khalil Khalaf

Reputation: 9407

What is the difference between Dictionary.Item and Dictionary.Add?

Reading the accepted answer of C# Java HashMap equivalent, it literary states:

C#'s Dictionary uses the Item property for setting/getting items:

  • myDictionary.Item[key] = value
  • MyObject value = myDictionary.Item[key]

And when trying to implement it, I get an error when using:

myDictionary.Item[SomeKey] = SomeValue;

Error: CS1061 'Dictionary' does not contain a definition for 'Item'

And I will need to use a myDictionary.Add(SomeKey, SomeValue); instead same as this answer and MSDN - Dictionary in order to resolve the error.

The code is fine, but out of curiosity am I doing anything wrong? Other than one does not compile, what is the difference between

Dictionary.Item[SomeKey] = SomeValue; 

and

Dictionary.Add(SomeKey, SomeValue);

Edit:

I edited the accepted answer in C# Java HashMap equivalent. See edition history to know why.

Upvotes: 2

Views: 240

Answers (2)

Evk
Evk

Reputation: 101483

Difference is simple

Dictionary[SomeKey] = SomeValue; // if key exists already - update, otherwise add
Dictionary.Add(SomeKey, SomeValue); // if key exists already - throw exception, otherwise add

As for the error

Error: CS1061 'Dictionary' does not contain a definition for 'Item'

C# allows for "default" indexer, but how it should be implemented internally? Remember that there are many languages working with CLR, not just C#, and they would also need a way to call that indexer.

CLR has properties, and it also allows to provide arguments when those properties getters or setters are called, because properties are really compiled as a pair of get_PropertyName() and set_PropertyName() methods. So, indexer can be represented by a property which getter and setter accept additional arguments.

Now, there cannot be property without a name, so we need to choose a name for the property which represents our indexer. By default, "Item" property is used for the indexer property, but you can overwrite it with IndexerNameAttribute.

Now when indexer is represented as regular named property, any CLR language can called it with get_Item(index).

That's why in article you linked that indexer is referenced by Item. Though when you use it from C#, you have to use appropriate syntax and just call it as

Dictionary[SomeKey] = SomeValue;

Upvotes: 1

Huu Thien Tan Nguyen
Huu Thien Tan Nguyen

Reputation: 184

I think the difference is:

  • Dictionary[SomeKey] = SomeValue; (not Dictionary.Item[SomeKey] = SomeValue;) will add new key value pair if the key does not exist, if the key existed, it will replace the value
  • Dictionary.Add(SomeKey, SomeValue); will add new key value pair, and if the key already existed, it will throw Argument Exception: An item with the same key has already been added

The example is:

try
{
     IDictionary<int, string> dict = new Dictionary<int, string>();

     dict.Add(0, "a");
     dict[0] = "b";  // update value to b for the first key
     dict[1] = "c";  // add new key value pair
     dict.Add(0, "d"); // throw Argument Exception
 }
 catch (Exception ex)
 {
     MessageBox.Show(ex.Message);
 }

Upvotes: 2

Related Questions