Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Dictionaries? | Additional Structures & File Handling
course content

Зміст курсу

C# Beyond Basics

What are Dictionaries?What are Dictionaries?

In Arrays, we access data through indexing (arrayName[index]). In an Array, every value (element) has a unique index, which is used for accessing that value, therefore we can say that an Array has an index-value structure.

There's a similar structure called a Dictionary, in which we have key-value pairs instead. While an index is always an integer number, a key can be of any basic data type, however it's commonly a string.

The following illustration shows an example illustration of dictionary which stores the number of different fruits:

1. Creating a Dictionary

We can declare a dictionary using the following syntax:

cs

index.cs

Here keyDataType represents the data type of the key while the valueDataType represents the data type of the values. dictionaryName is the name of the dictionary.

An implicit declaration is also valid:

cs

index.cs

2. Adding Data

We can use the Add method to add items to the dictionary:

cs

index.cs

3. Accessing Data

We can access the data in dictionaries using the keys:

cs

index.cs

Following is an example which demonstrates all three:

cs

index.cs

Each key in a dictionary must be unique. In case we try to add a key which already exists, the compiler will show an error.

In Dictionaries, Count attribute shows the number of key-value pairs stored in it. Remove method takes in a key and removes that key-value pair from the dictionary. Clear method simply removes all key-value pairs from a dictionary. It will be a good code reading exercise to read and understand the usage of Count, Remove and Clear from the following code:

cs

index.cs

1. What module must be imported for using dictionaries?
2. What is the correct syntax for creating a dictionary?
3. What will be the output of the following code?

What module must be imported for using dictionaries?

Виберіть правильну відповідь

What is the correct syntax for creating a dictionary?

Виберіть правильну відповідь

What will be the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 4
course content

Зміст курсу

C# Beyond Basics

What are Dictionaries?What are Dictionaries?

In Arrays, we access data through indexing (arrayName[index]). In an Array, every value (element) has a unique index, which is used for accessing that value, therefore we can say that an Array has an index-value structure.

There's a similar structure called a Dictionary, in which we have key-value pairs instead. While an index is always an integer number, a key can be of any basic data type, however it's commonly a string.

The following illustration shows an example illustration of dictionary which stores the number of different fruits:

1. Creating a Dictionary

We can declare a dictionary using the following syntax:

cs

index.cs

Here keyDataType represents the data type of the key while the valueDataType represents the data type of the values. dictionaryName is the name of the dictionary.

An implicit declaration is also valid:

cs

index.cs

2. Adding Data

We can use the Add method to add items to the dictionary:

cs

index.cs

3. Accessing Data

We can access the data in dictionaries using the keys:

cs

index.cs

Following is an example which demonstrates all three:

cs

index.cs

Each key in a dictionary must be unique. In case we try to add a key which already exists, the compiler will show an error.

In Dictionaries, Count attribute shows the number of key-value pairs stored in it. Remove method takes in a key and removes that key-value pair from the dictionary. Clear method simply removes all key-value pairs from a dictionary. It will be a good code reading exercise to read and understand the usage of Count, Remove and Clear from the following code:

cs

index.cs

1. What module must be imported for using dictionaries?
2. What is the correct syntax for creating a dictionary?
3. What will be the output of the following code?

What module must be imported for using dictionaries?

Виберіть правильну відповідь

What is the correct syntax for creating a dictionary?

Виберіть правильну відповідь

What will be the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 4
some-alt