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

사전의 첫 번째 요소에 액세스하는 C# 프로그램

<시간/>

다음은 일부 요소가 포함된 사전입니다 -

Dictionary<int, string> d = new Dictionary<int, string>() {
   {1,"Electronics"},
   {2, "Clothing"},
   {3,"Toys"},
   {4,"Footwear"},
   {5, "Accessories"}
};

이제 첫 번째 요소를 표시하려면 다음과 같이 키를 설정합니다.

d[1];

위는 첫 번째 요소를 표시합니다.

using System;
using System.Collections.Generic;
public class Program {
   public static void Main() {
      Dictionary<int, string> d = new Dictionary<int, string>() {
         {1,"Electronics"},
         {2, "Clothing"},
         {3,"Toys"},
         {4,"Footwear"},
         {5, "Accessories"}
      };
      foreach (KeyValuePair<int, string> ele in d) {
         Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
      }
      Console.WriteLine("First element: "+d[1]);
   }
}

출력

Key = 1, Value = Electronics
Key = 2, Value = Clothing
Key = 3, Value = Toys
Key = 4, Value = Footwear
Key = 5, Value = Accessories
First element: Electronics