如何在 C# 中将列表初始化为空列表?
csharpprogrammingserver side programming更新于 2025/5/1 12:52:17
要在 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!