C# 中的空列表

csharpprogrammingserver side programming更新于 2025/11/25 10:03:00

设置一个包含零个元素的列表 −

List<string> myList = new List<string>();

现在检查列表是否为空或为 null −

Console.WriteLine(myList == null);

以上,返回 “False” 即列表不为 null - 列表为空。

让我们看看完整的代码 −

示例

using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
   public static void Main() {
      List<string> myList = new List<string>();
      // 返回 false,即空列表(不是空列表)
      Console.WriteLine(myList == null);
   }
}

输出

False

相关文章