Computer >> Máy Tính >  >> Lập trình >> C#

Làm thế nào để tạo một OrderDictionary trong C #?

Để tạo một OrderDictionary trong C #, mã như sau -

Ví dụ

 using System; using System.Collections; using System.Collections.Specialized; public class Demo {public static void Main () {OrderedDictionary dict =new OrderedDictionary (); dict.Add ("A", "Sách"); dict.Add ("B", "Điện tử"); dict.Add ("C", "Thiết bị đeo thông minh"); dict.Add ("D", "Vật nuôi"); dict.Add ("E", "Quần áo"); dict.Add ("F", "Giày dép"); Console.WriteLine ("Các phần tử theo thứ tự ..."); foreach (DictionaryEntry d in dict) {Console.WriteLine (d.Key + "" + d.Value); } Console.WriteLine ("Số phần tử trong OrderedDictionary =" + dict.Count); dict.Remove ("E"); Console.WriteLine ("Số phần tử trong OrderDictionary (Đã cập nhật) =" + dict.Count); }} 

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Các phần tử có thứ tự 

Ví dụ

Hãy để chúng tôi xem một ví dụ khác -

 using System; using System.Collections; using System.Collections.Specialized; public class Demo {public static void Main () {OrderedDictionary dict =new OrderedDictionary (); dict.Add ("1", "Một"); dict.Add ("2", "Hai"); dict.Add ("3", "Ba"); dict.Add ("4", "Bốn"); dict.Add ("5", "Năm"); dict.Add ("6", "Sáu"); dict.Add ("7", "Bảy"); dict.Add ("8", "Tám"); ICollection col =dict.Keys; String [] strKeys =new String [dict.Count]; col.CopyTo (strKeys, 0); Console.WriteLine ("Đang hiển thị các phím ..."); for (int i =0; i  

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Hiển thị các phím ... 12345678