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

Xóa mục nhập có khóa được chỉ định khỏi OrderDictionary trong C #

Để xóa mục nhập có khóa được chỉ định khỏi OrdererdDictionary 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 ("A", "Một"); dict.Add ("B", "Hai"); dict.Add ("C", "Ba"); dict.Add ("D", "Bốn"); 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 ("C"); dict.Remove ("D"); 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ó trật tự ...