What is Dictionary as Map in C# in Ubuntu 20.04?
A dictionary is a sort of collection that contains key/value pairs. Dictionary has the advantage that it is of generic type. The system library “system.Collection.Generic namespace” is used to define a term dictionary. The Dictionary in C# programming language is dynamic, which means that the size of the dictionary expands in response to demand.
Syntax of the Dictionary C# in Ubuntu 20.04
The general syntax of the Dictionary in C# is given below:
The “Tkey” and “Tvalue” are type parameters that represent “Tkey” for type key and “TValue” for the type of the value in the dictionary.
Important features of the Dictionary C# in Ubuntu 20.04
- The key in the Dictionary cannot be blank, but the value can.
- In the dictionary, a key must be one-of-a-kind. Duplicate keys are not permitted, and if we attempt to use duplicate keys, then we will throw an exception at compilation time.
- Only the same types of items can be stored in the Dictionary.
- The Dictionary holds the number of components in the Dictionary as its capacity.
- In C# Dictionary, the IDictionary<TKey, TValue> interface is implemented.
How to utilize the Dictionary C# in Ubuntu 20.04
We have three ways in C# to get at the Dictionary’s key/value pair through the for loop, foreach loop, and specifying the index value. Take a look at the examples below to get a better understanding of how to use the Dictionary as a map in C#:
Example 1: Creating the Dictionary in C# in Ubuntu 20.04
Let’s look at an example of a generic Dictionary<TKey, TValue> class that uses the Add() function to store elements and for-each loop to iterate them.
Firstly, we must import the system libraries using the keyword “using”. This is an example of a C# dictionary, so it is necessary to import the “System.Collection.Generics” library under which the dictionary term is defined. Then we created our C# class and named it “Program”. The class invoked the main method, and in the main method, we have called a “Dictionary<Tkey, TValue>” class for creating the dictionary.
Here, the “Map_dict1” is used as a “Dictionary<int, string>” to store dictionary types. The “int” represents “Tkey”, which is key, and the “string” represents “TValue”, which is the value of the dictionary. The Add() function is used to add the key/value pairs in the dictionary “Map_dict1”. Through the foreach loop, we will access the key/value pairs of the dictionary “Map_dict1”. We have another “Dictionary<Tkey, TValue>” which has created another dictionary “, My_Dict2”, and added elements in the dictionary without using the Add() function. Note that in the foreach loop, we have the “KeyValuePair” method class, which will help us access the key/value pairs of the two specified dictionaries.
The following are the result values of the dictionaries created in the above code.
Example 2: Removing the elements in C# in Ubuntu 20.04
The Dictionary<TKey, TValue> type includes two methods for removing elements, which are the remove() and clear() methods. The value with the given key is removed from the Dictionary<TKey, TValue> by the remove() method. On the other hand, the clear() method clears the Dictionary<TKey, TValue> all keys and values.
The Dictionary<TKey, TValue> stores the dictionary key/values pair and represents it as a Map in the above code. First, the elements are added to the dictionary using the Add() function. The “KeyValuePair” class is used in the foreach loop to get the key/value pairs of the dictionary “Map”. Then, we invoke the remove() method and pass the key “4” to it, removing the key-value pair of the specified dictionary key value. After the remove() method, we are accessing the key-value pair of the dictionary using “KeyValuePair” in the foreach loop. At last, we have the clear() method called for deleting all the items present in the dictionary.
The output from the remove() method and clear() method is seen in the following image.
Example 3: Checking the availability of the elements of the Dictionary in C# in Ubuntu 20.04
We can also use a Dictionary to see if a specific key or value exists in the given dictionary through ContainsKey and ContainsValue.
The dictionary “Map_dict” is created by the Dictionary<TKey, Tvalue> and stores the “The” of int and “TValue” of String type. The Addg() method adds the elements in the given dictionary. To check the key of the specified dictionary, we have called the Conatinskey method. The ConstinsKey method will check if the Dictionary<TKey, TValue> includes the key of the dictionary. We have also used a ContainsValue method in the above code to check the value of the specified key of the dictionary. These methods are invoked in the if-else condition bypassing the key and value present in the dictionary “Map_dict”.
Hence, both the dictionary’s key and value are available, so the output shows the result under true conditions.
Example 4: Updating the elements in the Dictionary in C# in Ubuntu 20.04
To update the value of a Dictionary<TKey, TValue> key, we can create a key in the indexer. Let’s update the dictionary by specifying the key index of the dictionary.
The C# class is defined as “MapDictionary”, which we have called its main function. In the main function, we have a variable declaration as “Name”, which is the dictionary, and the variable has a Dictionary<TKey, <TValue> constructor called. The elements are set in the dictionary. After that, we have given a key index of the variable “Name” and updated the values to the corresponding keys. The foreach loop will help us get the updated values of the key in the dictionary.
The values of the specified key index are updated on the terminal screen.
Conclusion
As C# has no built-in map, we used a dictionary instead. The article has given you a clear concept of dictionaries and how we can use them as a map. We’ve gone through a dictionary’s definition and some of its key features. Then, we have four examples of using a dictionary that demonstrate how to create the dictionary, access its elements, and remove and update elements in the dictionary. I hope you found this artifact beneficial in dealing with the C# map.