如何在 C# 中将列表初始化为一个空列表?
如需将列表初始化为 C# 中的空列表,请按以下语句进行设置,不含任何元素 −
List<string> list = new List<string>();
现在,使用 Any() 方法 检查列表是否为空 −
bool chk = !list.Any();
让我们看看完整代码 −
示例
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { // empty list List<string> list = new List<string>(); // check for empty list bool chk = !list.Any(); if(chk) { Console.WriteLine("List is Empty!"); } else { Console.WriteLine("List isn't Empty!"); } } }
输出
List is Empty!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP