ในการเริ่มต้นพจนานุกรมเป็นพจนานุกรมเปล่า ให้ใช้เมธอด Clear() มันล้างพจนานุกรมและทำให้ว่างเปล่า
dict.Clear();
หลังจากนั้น ใช้คุณสมบัติการนับพจนานุกรมเพื่อตรวจสอบว่ารายการว่างหรือไม่ -
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!