Dictionary in C#

Introduction to Dictionary in C#

Dictionary is a collection type which is present in the built-in library of C#. It is named as Dictionary because it works just like a Dictionary. As in Dictionary, we have keys in the form of alphabets by which we search the meaning of a particular word among the words associated with the key value which is an alphabet. In C# the Dictionary data structure works in the same manner. There are the pairs of values associated with a key just as there are some words associated with an alphabet in English Dictionary. With the help of a key value, a certain pair of values can be searched or can be accessed. To explain this let us consider the following Example.

Including Generics Class

You first have to include the library in which Dictionary is located in the following manner:

Note: This library should not only be included in your Main() but also in each class associated with your program.

Now as I have pre-defined data sets. So the inside brackets of Student Class, I will have to define the get & set function in the following manner. First I have to define the functions in which I want to set the get & set functions in the following way:

Create Getters and Setters

As I have a Student class so, the functions will be.

public int ID { get; set; }
public string Name { get; set; }
public int Fee { get; set; }
Now to explain my other functions I will select on function for demonstration which is

public int ID { get; set; }

Now public means that my function is visible to all the classes related to my program and the data which is set in this function can be retrieved by all the other classes and the classes don’t need any permission to retrieve the data set in this function. int means that the return type of my function is an integer. It means that my function will return an Integer value. ID is the name of my function. { get; set; } shows that my function is setting data of ID by set function and getting data of ID by get function. All the other function will work in the same manner.

Now as for my program, the data sets are pre-defined. So, I will define the data sets in Main() of my program in the following manner.

Dictionary Compare

Now Add another class with the name of StudentDictionaryComparer and type the following code in it:

IEqualityComparer is a comparer which compares the key values with the data sets and by those key values, it accesses the data sets.

public bool means that the function is public and is accessible from any class and by Main() in the program. KeyValuePair<int,Student> x means that a data set is assigned to variable x. KeyValuePair. y means that all the data sets in which the selected data set will have to be searched are assigned the variable y. Now the data pair selected in x variable matches a data pair of Y variable then bool will return true otherwise, it will return false. This code will do the above-mentioned work.

Now the above-mentioned code provides the key of the data pair and the HashCode of the data set whose data pair is matched. Basically, this function is providing keys of the data sets which can be used to transfer data sets from one location to another location.

Create Student Dictionary Object

Now in the Main{} you will write the following code:

IDictionary shows the data structure you are using which means that you are using a Dictionary type of data structure. <int, Student> represents the key values. studentDict is the instance of Student class and Dict shows that this instance of the class is of Dictionary type. new Dictionary is the instance of Dictionary data structure. () shows that we are using a function from the StudentDictionarComparer class and this function is public bool Equals().

1 is the key value of the above-mentioned data set. new Student () is the instance of Student class.

This line shows the entries on which the data sets are to be searched which are 1&Ali.

This shows that 1 & Ali will be searched on 1.

To do the tasks of comparing the following function of StudentDictionaryComparer class is called StudentDictionaryComparer() by typing the following:

In bool result variable the result of the matched data set will be stored.

Another Scenario:

Now suppose that we don’t have pre-defined data sets. Now in this scenario we will take the data sets from the user in the following way:

You first have to include the library in which Dictionary is located in the following manner:

Note: This library should not only be included in your Main() but also in each class associated with your program.
Now as I have pre-defined data sets. So, my C# program for Dictionary will be as follows:

After the inside brackets of Student Class, I will have to define the get & set function in the following manner. First I have to define the functions in which I want to set the get & set functions in the following way:

Getters and Setters

As I have a Student class so, the functions will be.

public int ID { get; set; }
public string Name { get; set; }
public int Fee { get; set; }

Now to explain my other functions I will select on function for demonstration which is:

Now public means that my function is visible to all the classes related to my program and the data which is set in this function can be retrieved by all the other classes and the classes don’t need any permission to retrieve the data set in this function. int means that the return type of my function is an integer. It means that my function will return an Integer value. ID is the name of my function. { get; set; } shows that my function is setting data of ID by set function and getting data of ID by get function. All the other function will work in the same manner.
Now as for my program, the data sets are pre-defined. So, I will define the data sets in main() of my program in the following manner.

Now Add another class with the name of StudentDictionaryComparer and type the following code in it:

 

IEqualityComparer is a comparer which compares the key values with the data sets and by those key values it accesses the data sets.

public bool means that the function is public and is accessible from any class and by main() in the program. KeyValuePair<int,Student> x means that a data set is assigned to variable x. KeyValuePair y means that all the data sets in which the selected data set will have to be searched are assigned the variable Y. Now the data pair selected in x variable matches a data pair of Y variable then bool will return true otherwise, it will return false. This code will do the above-mentioned work.

 

Now the above-mentioned code provides the key of the data pair and the HashCode of the data set whose data pair is matched. This function is providing keys of the data sets which can be used to transfer data sets from one location to another location.

Now Main() will be defined in the following way:

 

Read more about Generics, Collections in C#
Read more about Queue in C#