如何在 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!

更新于: 06-09-2023

45K+ 浏览量

开启你的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.