Step 1: Implementation
The constructor of the dictionary in C# takes in two parameters, key and value. The following program implement dictionary that contains key of data type string and value of data type integer. The Dictionary is created to store the days of the week along with their values from 1 to 7. Thus, the value “Monday” is paired with the key “1” and so on.
The Add() method is used to populate the dictionary class created. The object Days reference the dictionary class. The Add() method takes in two parameters, the key and the value. The elements of a dictionary can be iterated using the foreach statement.
Step 2: Output
The foreach statement loops through the elements of the object Days. When the program is executed, the key along with its paired value gets printed one after another.
Like other collections, the dictionaries in C# also contain methods like remove, clear and other collection methods.