Hi, today as part of the exercises we will do a dictionary project that uses a lot of collections and lists, but the dictionary list in C # looks a bit different, we’ll get to that, but this time we will no longer work in the console, we will use now a more professional environment Windows Forms Application, to work!
Now we have become professional programmers and the console is not enough for us. We need something more advanced. Our need comply with the environment that I mentioned earlier. You can create buttons, text boxes, etc. with drag-and-drop methods. We only need to encode the controls we place there.
Let’s create a project called Dictionary, it is created in the same way as an ordinary console application, you just have to press where the name of the environment in which we will work is displayed.
Now let’s go to the dictionary.
A dictionary in C# is created in this way:
Dictionary <TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
Where TKey simply means the type of keywords that will be there and TValue the type of dictionary values.
Let’s present this dictionary in action, we add some code to the button, but we have not added any button so let’s go to the Toolbox tab on the left and choose a button and drag it to the form, if you do not see the Toolbox tab in your IDE go to the View tab at the top and there you will see the Toolbox tab, let’s move on ….
We have a button so let’s add some code to it, click on the button twice, the method of this button will be created, when we click on the button the code will be executed in this method, so let’s add this code to this method:
Dictionary<string, string> wordDefinition = new Dictionary<string, string>();
wordDefinition.Add("Dictionary", "A set of words arranged and developed according to a certain principle, usually explained in terms of meaning.");
wordDefinition.Add("Key", "A method or thing that allows you to achieve or understand something.");
wordDefinition.Add("Value", "A number that specifies how many units a given physical quantity or size that can replace an algebraic expression.");
What it is doing? First, we create a dictionary in it, then as we do in the lists using the Add method, we add a definition to the dictionary, first the word then the definition of the word, lower in the code in the if() statement we check using the ContainsKey() function if the word key in the dictionary exists, if there is we display it the Messagebox function.
This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-16-dictionary-part-1
As you can see, dictionaries are very similar to lists, have a similar function, they work similarly, I will describe the rest of these functions, but in the next part of this lesson, see you!