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

Nhận một điều tra viên lặp qua HybridDictionary trong C #

Để có được một điều tra viên lặp qua HybridDictionary, mã như sau -

Ví dụ

 using System; using System.Collections; using System.Collections.Specialized; public class Demo {public static void Main () {HybridDictionary dict1 =new HybridDictionary (); dict1.Add ("A", "Sách"); dict1.Add ("B", "Điện tử"); dict1.Add ("C", "Thiết bị đeo thông minh"); dict1.Add ("D", "Vật nuôi"); dict1.Add ("E", "Quần áo"); dict1.Add ("F", "Giày dép"); Console.WriteLine ("Các phần tử HybridDictionary1 ..."); foreach (DictionaryEntry d in dict1) {Console.WriteLine (d.Key + "" + d.Value); } HybridDictionary dict2 =new HybridDictionary (); dict2.Add ("1", "Một"); dict2.Add ("2", "Hai"); dict2.Add ("3", "Ba"); dict2.Add ("4", "Bốn"); dict2.Add ("5", "Năm"); dict2.Add ("6", "Sáu"); Console.WriteLine ("\ nHybridDictionary2 cặp khóa-giá trị ..."); IDictionaryEnumerator demoEnum =dict2.GetEnumerator (); while (demoEnum.MoveNext ()) Console.WriteLine ("Key =" + demoEnum.Key + ", Value =" + demoEnum.Value); }} 

Đầu ra

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

Các phần tử
 HybridDictionary1 ... A SáchB Điện tửC Thiết bị đeo thông minhD Đồ dùng cho vật nuôiE Quần áoF Giày dépHybridDictionary2 các cặp khóa-giá trị ... Key =1, Value =OneKey =2, Value =TwoKey =3, Value =ThreeKey =4, Value =FourKey =5, Giá trị =FiveKey =6, Giá trị =Sáu 

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 () {HybridDictionary dict =new HybridDictionary (); dict.Add ("A", "Gary"); dict.Add ("B", "Andy"); dict.Add ("C", "Dấu"); dict.Add ("D", "Barry"); dict.Add ("E", "Katie"); dict.Add ("F", "John"); Console.WriteLine ("Các cặp khóa-giá trị HybridDictionary ..."); IDictionaryEnumerator demoEnum =dict.GetEnumerator (); while (demoEnum.MoveNext ()) Console.WriteLine ("Key =" + demoEnum.Key + ", Value =" + demoEnum.Value); }} 

Đầu ra

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

 HybridDictionary key-value ... Key =A, Value =GaryKey =B, Value =AndyKey =C, Value =MarkKey =D, Value =BarryKey =E, Value =KatieKey =F, Value =John