Dictionary in C#

Introduction:

Dictionary class represent a collection of key values pair of Data. This class is generic type so this class can store any type of data. It can be primitive data types or non-primitive data types. First of all we will discuss about primitive data types then we will discuss about non-primitive data types.

Act:

We can Add, Remove, Clear and other collection methods. The Key is used for get value from in a Dictionary.

Primitive data types:

The following code snippet creates a Dictionary named “students” where keys Is integer and value is string.

Add values into students of Dictionary.

Read all data

Output

 

 

Non-primitive data types:

Now we will create an Employee class Bellow is the following Class.

The following code snippet creates a Dictionary named “employees” where keys Is integer and value is Employee type. Employee type is a custom create type.

Value assign employee’s object

Add values into employees of Dictionary.

Read all data

Output

 

 

Count number of items of dictionary.

Remove item from dictionary.

Clear the Dictionary items

 

Leave a Reply

Your email address will not be published. Required fields are marked *