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

วิธีสร้าง Dictionary ว่างใน C#

ใช้ Clear() ล้าง Dictionary ให้ว่าง:

dict.Clear();

ตรวจสอบด้วย Count:

if (dict.Count == 0) {
    Console.WriteLine("Dictionary is empty!");
}

ตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo {
    public class Program {
        public static void Main(string[] args) {
            var dict = new Dictionary<string, string>();
            dict.Clear();
            if (dict.Count == 0) {
                Console.WriteLine("Dictionary is empty!");
            }
        }
    }
}

ผลลัพธ์

Dictionary is empty!