C# dictionary get value

C# dictionary get value

The Dictionary is a fast way to remember things. A key (string) maps to a value (int). Add() inserts keys and values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        
        var dictionary = new Dictionary<string, int>();
        dictionary.Add("a", 2);
        dictionary.Add("b", 1);
        dictionary.Add("c", 0);
        dictionary.Add("d", -2);
        dictionary.Add("e", -3);
        // The dictionary has 5 pairs.
        Console.WriteLine("Dictonary 1: " + dictionary.Count);


        var dictionary2 = new Dictionary<string, int>()
        {
            {"a", 2},
            {"b", 1},
            {"c", 0},
            {"d", -2},
            {"e", -3}
        };
        // This dictionary has 5 pairs.
        Console.WriteLine("Dictonary 2: " + dictionary2.Count);
        Console.ReadKey();
    }
}

2021-10-03