C# 中的空列表


設定一個有 0 個元素的列表 -

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>();
      // returns false i.e. an empty list (not a null list)
      Console.WriteLine(myList == null);
   }
}

輸出

False

更新日期:2020 年 6 月 22 日

8000+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲取認證

開始行動
廣告
© . All rights reserved.