Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีสร้าง List ว่างใน C#

สร้าง List โดยไม่ใส่ข้อมูล:

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() {
        List<string> list = new List<string>();
        bool chk = !list.Any();
        if(chk) {
            Console.WriteLine("List is Empty!");
        } else {
            Console.WriteLine("List isn't Empty!");
        }
    }
}

ผลลัพธ์

List is Empty!