Home › C# › C# Dictionaries
C# · Jul 28, 2016 · 1 MIN READ

C# Dictionaries

Dictionaries are used in C# to define key value pairs. They are part of System.Collections.Generic.

The format is:

Dictionary<key,value>

 

To define a dictionary, use the format:

Dictionary<datatype, datatype> d = new Dictionary<datatype, datatype>();

For example:

Dictionary<int, int> d = new Dictionary<int, int>();

or

Dictionary<string, int> d = new Dictionary<string, int>();

To add to the data type, you can do either:

Dictionary<string, int> d1 = new Dictionary<string, int>();
d1.Add("Bob", 12345);

or

d[3] = 30;

From there, you can use different methods on the dictionary such as ContainsKey, ContainsValue:

if (d.ContainsKey(2))
{
    Console.WriteLine(d[2]);
}

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

Leave a Reply

Your email address will not be published. Required fields are marked *

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.