如何使用 C# 将词典初始化为一个空词典?
要将词典初始化为一个空词典,请使用 Clear() 方法。它将清除词典并将其格式化为一个空词典。
dict.Clear();
在此之后,使用 Dictionary 计数属性来检查列表是否为空 -
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!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP