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

C#에서 사전을 빈 사전으로 초기화하는 방법은 무엇입니까?

<시간/>

사전을 빈 사전으로 초기화하려면 Clear() 메서드를 사용하십시오. 사전을 지우고 빈 상태로 만듭니다.

dict.Clear();

그런 다음 Dictionary count 속성을 사용하여 목록이 비어 있는지 여부를 확인하십시오 -

if (dict.Count == 0) {
   Console.WriteLine("Dictionary is empty!");
}

전체 코드를 보자 -

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

namespace Demo {
   public class Program {
      public static void Main(string[] args) {

         var dict = new Dictionary < string, string > ();
         dict.Clear();
         if (dict.Count == 0) {
            Console.WriteLine("Dictionary is empty!");
         }
      }
   }
}

출력

Dictionary is empty!