พจนานุกรมคือชุดของคีย์และค่าต่างๆ ใน C # พจนานุกรมรวมอยู่ในเนมสเปซ System.Collection.Generics
เพื่อประกาศและเริ่มต้นพจนานุกรม -
IDictionary<int, int> d = new Dictionary<int, int>();
ด้านบน ประเภทของคีย์และค่าถูกตั้งค่าในขณะที่ประกาศอ็อบเจ็กต์พจนานุกรม int เป็นประเภทของคีย์ และ string เป็นประเภทของค่า ทั้งสองจะถูกเก็บไว้ในวัตถุพจนานุกรมชื่อ d.
เรามาดูตัวอย่างกัน −
ตัวอย่าง
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, int> d = new Dictionary<int, int>();
d.Add(1,97);
d.Add(2,89);
d.Add(3,77);
d.Add(4,88);
// Dictionary elements
Console.WriteLine("Dictionaly elements: "+d.Count);
}
}