Dictionary Data Type in C#

Category > CSHARP || Published on : Tuesday, May 12, 2015 || Views: 5456 || Dictionary Data Type in C# Dictionary Data Type dictionary data type in c#


In the this tutorial, we are going to learn Dictionary data type in C#. Dictionary Data type stores key value pairs,  We’ll learn the differences and some of the more common methods associated with Dictionary in this tutorial.

So lets start the fun part that is the coding

Step 1: Create a Winform Project

Step 2: By default in solution explorer we have a form with name "Form1". Add 2 buttons from toolbox to "Form1".

Step 3:  Write the Code below into the button1 click event. This will  used to declare a new Dictionary object, we’ll initialize it from the Dictionary class,The code above specifies the key and value types respectively. This is required by the C# syntax, and is one of the major differences between the VB version. We can add key value pairs to our Dictionary by using the Add method, let’s take a look:

var variable = new Dictionary<string, string>();
 
variable.Add("key1","value1");
variable.Add("key2","value2");

Step 4:  Write the Code below into the button2 click event.  We can read data from the Dictionary object like so:

MessageBox.Show(variable.Item("key2"));

The above code will bring alertbox  with the string “value2?.

Conclusion:-
so you have learned Dictionary data type in C# with Winform Example.... Happy Coding !!!!