Introduction

In this quick tutorial, we’re going to be over the Dictionary data type in C#. This data type stores key value pairs, but unlike VB, it’s a little more strict in C#. We’ll learn the differences and some of the more common methods associated with Dictionary in this tutorial.

Put It Into Practice

To declare a new Dictionary object, we’ll initialize it from the Dictionary class like so:

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:

We can read data from the Dictionary object like so:

The above code will output the string “value2”.

Those are the most common forms you will see and use the Dictionary data type, for more methods and usage visit the Microsoft MSDN Documentation!