Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Dictionary ใน C# คืออะไร

Dictionary เป็น Collection ของ Key-Value Pairs อยู่ใน Namespace System.Collections.Generic

การประกาศและ Initialize

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

Key เป็น int, Value เป็น int เก็บใน Object 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);
        Console.WriteLine("Dictionary elements: " + d.Count);
    }
}