Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ลำดับชั้นพจนานุกรมใน C #


คลาส OrderedDictionary แสดงถึงชุดของคู่คีย์/ค่าที่คีย์หรือดัชนีสามารถเข้าถึงได้

ต่อไปนี้เป็นคุณสมบัติของคลาส OrderedDictionary -

Sr.no คุณสมบัติ &คำอธิบาย
1 นับ
รับจำนวนคู่ของคีย์/ค่าที่มีอยู่ในคอลเลกชัน OrderedDictionary
2 เป็นแบบอ่านอย่างเดียว
รับค่าที่ระบุว่าคอลเลกชัน OrderedDictionary เป็นแบบอ่านอย่างเดียวหรือไม่
3 รายการ[Int32]
รับหรือตั้งค่าที่ดัชนีที่ระบุ
4 รายการ[วัตถุ]
รับหรือตั้งค่าด้วยคีย์ที่ระบุ
5 กุญแจ
รับอ็อบเจ็กต์ ICollection ที่มีคีย์ในคอลเล็กชัน OrderedDictionary
6 ค่า
รับวัตถุ ICollection ที่มีค่าในคอลเลกชัน OrderedDictionary

ต่อไปนี้เป็นวิธีการบางส่วนของคลาส OrderedDictionary -

Sr.no วิธีการ &คำอธิบาย
1 เพิ่ม (วัตถุ, วัตถุ)
เพิ่มรายการด้วยคีย์และค่าที่ระบุลงในคอลเล็กชัน OrderedDictionary ที่มีดัชนีต่ำสุดที่พร้อมใช้งาน
2 AsReadOnly()
ส่งกลับสำเนาคอลเลกชัน OrderedDictionary แบบอ่านอย่างเดียวในปัจจุบัน
3 ล้าง()
ลบองค์ประกอบทั้งหมดออกจากคอลเลกชัน OrderedDictionary
4 ประกอบด้วย (วัตถุ)
กำหนดว่าคอลเลกชัน OrderedDictionary มีคีย์เฉพาะหรือไม่
5 CopyTo(Array, Int32)
คัดลอกองค์ประกอบ OrderedDictionary ไปยังออบเจ็กต์ Array แบบหนึ่งมิติที่ดัชนีที่ระบุ
6 เท่ากับ (วัตถุ)
กำหนดว่าวัตถุที่ระบุเท่ากับวัตถุปัจจุบันหรือไม่ (สืบทอดจากวัตถุ)
7 GetEnumerator()
ส่งกลับวัตถุ IDictionaryEnumerator ที่วนซ้ำผ่านคอลเลกชัน OrderedDictionary

เรามาดูตัวอย่างกัน −

ตัวอย่าง

เพื่อให้ได้จำนวนของคู่คีย์/ค่าที่มีอยู่ใน OrderedDictionary รหัสจะเป็นดังนี้ -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Home Appliances");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

OrderedDictionary elements...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0

ตัวอย่าง

ในการลบองค์ประกอบทั้งหมดออกจาก OrderedDictionary รหัสจะเป็นดังนี้ -

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Books");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");
      Console.WriteLine("OrderedDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
      dict.Clear();
      Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated)= 0