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!