Dictionary stores key - value pairs. The keys are unique, can't be modified but can be deleted.


In order to use it, the following line needs to be added:


Inserting Data

The inserted data must correspond to the key's and values type.


In this example, 2 records have been inserted with string keys and int values.

If we uncomment the last line, an exception will be thrown.

Accessing Values

Values can be accessed via their keys.

Let's look at the following example:


The key-value pair for Coke is set at line 11 and accessed at line 14.

Not Set Key

Accessing key that's not set in Exception.

Let's look at the following example:

The map doesn't contain any Coke key, so exception is thrown.

Iterating Elements

Let's have a program in which students scores are stored in dictionary and printed afterwards.


Removing Elements

That's done via the Remove function which accepts key as argument. Like in this example:


Size is the count of the key-value pairs.

It can be accessed via the Count function.

Let's look at this example:


4 records were inserted and 1 is deleted afterwards. This makes for 3 key-value pairs.