HOWTO · Csharp

C#에서 목록이 비어 있는지 확인

C#에서 목록이 비어 있는지 여부를 확인하는 데 사용할 수있는 두 가지 주요 메서드는 List.Count 메서드와 List.Any() 함수입니다.

이 페이지의 내용

이 자습서에서는 C#에서 목록이 비어 있는지 여부를 확인하는 방법을 소개합니다.

C#의List.Count속성을 사용하여 목록이 비어 있는지 확인

List.Count속성은 C#의 목록 안에있는 요소. 목록이 비어 있으면List.Count0입니다. 다음 코드 예제는 C#의List.Count속성을 사용하여 목록이 비어 있는지 여부를 확인하는 방법을 보여줍니다.

using System;
using System.Collections.Generic;
using System.Linq;

namespace check_empty_list {
  class Program {
    static void Main(string[] args) {
      List<string> emptyList = new List<string>();
      if (emptyList.Count == 0) {
        Console.WriteLine("List is Empty");
      } else {
        Console.WriteLine("Not Empty");
      }
    }
  }
}

출력:

List is Empty

위의 코드에서 빈 문자열 목록emptyList를 초기화하고 C#의List.Count속성으로 목록이 비어 있는지 여부를 확인합니다.

C#에서List.Any()함수를 사용하여 목록이 비어 있는지 확인

List.Any()함수를 사용하여 확인할 수도 있습니다. C#에서 목록이 비어 있는지 여부. List.Any()함수의 반환 유형은 부울입니다. 목록 안에 요소가 있으면List.Any()함수는true를 반환합니다. 그렇지 않으면false입니다. 아래 예제 코드를 참조하십시오.

using System;
using System.Collections.Generic;
using System.Linq;

namespace check_empty_list {
  class Program {
    static void Main(string[] args) {
      List<string> emptyList = new List<string>();
      if (emptyList.Any()) {
        Console.WriteLine("Not Empty");
      } else {
        Console.WriteLine("List is Empty");
      }
    }
  }
}

출력:

List is Empty

위의 코드에서 빈 문자열 목록emptyList를 초기화하고 C#의List.Any()함수를 사용하여 목록이 비어 있는지 여부를 확인합니다.