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

C#에서 지정된 키를 사용하여 HybridDictionary의 값을 가져오거나 설정합니다.

<시간/>

지정된 키를 사용하여 HybridDictionary의 값을 가져오거나 설정하려면 코드는 다음과 같습니다. -

예시

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      Console.WriteLine("Value at key 5 = "+myDict["5"]);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Value at key 5 = Notebook

예시

다른 예를 보겠습니다 -

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary myDict = new HybridDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      Console.WriteLine("Value at key 5 = "+myDict["5"]);
      myDict["5"] = "Smart Watches";
      Console.WriteLine("Value at key 5 [UPDATED] = "+myDict["5"]);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Value at key 5 = Notebook
Value at key 5 [UPDATED] = Smart Watches

예시

다른 예를 보겠습니다 -

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Queue queue = new Queue();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      queue.Enqueue(500);
      Console.WriteLine("Queue...");
      foreach(Object ob in queue) {
         Console.WriteLine(ob);
      }
      Console.WriteLine("Count of elements = "+queue.Count);
      Console.WriteLine("Synchronize access...");
      lock(queue.SyncRoot) {
         foreach(Object ob in queue) {
            Console.WriteLine(ob);
         }
      }
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Queue...
100
200
300
400
500
Count of elements = 5
Synchronize access...
100
200
300
400
500