Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

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!