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

รับตัวแจงนับที่วนซ้ำผ่าน ListDictionary ใน C #


เพื่อให้ได้ตัวแจงนับที่วนซ้ำผ่าน ListDictionary รหัสจะเป็นดังนี้ -

ตัวอย่าง

ใช้ System; ใช้ System.Collections; ใช้ System.Collections.Specialized; การสาธิตคลาสสาธารณะ { โมฆะคงที่สาธารณะ Main () { ListDictionary dict1 =ใหม่ ListDictionary (); dict1.Add("A", "หนังสือ"); dict1.Add("B", "อิเล็กทรอนิกส์"); dict1.Add("C", "อุปกรณ์สวมใส่อัจฉริยะ"); dict1.Add("D", "อุปกรณ์สำหรับสัตว์เลี้ยง"); dict1.Add("E", "เสื้อผ้า"); dict1.Add("F", "รองเท้า"); Console.WriteLine("ListDictionary1 องค์ประกอบ..."); foreach (DictionaryEntry d ใน dict1) { Console.WriteLine (d.Key + " " + d.Value); } ListDictionary dict2 =ใหม่ ListDictionary(); dict2.Add("1", "หนึ่ง"); dict2.Add("2", "สอง"); dict2.Add("3", "สาม"); dict2.Add("4", "สี่"); dict2.Add("5", "ห้า"); dict2.Add("6", "หก"); Console.WriteLine("\nListDictionary2 คู่คีย์-ค่า..."); IDictionaryEnumerator demoEnum =dict2.GetEnumerator (); ในขณะที่ (demoEnum.MoveNext()) Console.WriteLine("Key =" + demoEnum.Key + ", Value =" + demoEnum.Value); }}

ผลลัพธ์

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

ListDictionary1 elements...A BooksB ElectronicsC Smart WearablesD Pet SuppliesE ClothingF FootwearListDictionary2 key-value pairs...Key =1, Value =OneKey =2, Value =TwoKey =3, Value =ThreeKey =4, Value =FourKey =5, ค่า =FiveKey =6, ค่า =หก

ตัวอย่าง

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

ใช้ System; ใช้ System.Collections; ใช้ System.Collections.Specialized; การสาธิตคลาสสาธารณะ { โมฆะคงที่สาธารณะ Main () { ListDictionary dict =ใหม่ ListDictionary (); dict.Add("1", "หนึ่ง"); dict.Add("2", "สอง"); dict.Add("3", "สาม"); dict.Add("4", "สี่"); dict.Add("5", "ห้า"); dict.Add("6", "หก"); Console.WriteLine("ListDictionary คู่คีย์-ค่า..."); IDictionaryEnumerator demoEnum =dict.GetEnumerator (); ในขณะที่ (demoEnum.MoveNext()) Console.WriteLine("Key =" + demoEnum.Key + ", Value =" + demoEnum.Value); }}

ผลลัพธ์

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

คู่คีย์-ค่าของ ListDictionary...คีย์ =1, ค่า =OneKey =2, ค่า =TwoKey =3, ค่า =ThreeKey =4, ค่า =FourKey =5, ค่า =FiveKey =6, ค่า =Six