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

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

Để xóa mục nhập tại chỉ mục được chỉ định khỏi OrdererdDictionary, 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.RemoveAt (4); 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", "AB"); dict.Add ("2", "CD"); dict.Add ("3", "MN"); dict.Add ("4", "PQ"); 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.RemoveAt (1); dict.RemoveAt (2); 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ử theo thứ tựDictionary ... 1 AB2 CD3 MN4 PQ Số lượng phần tử trong OrderedDictionary =4 Số lượng phần tử trong OrderDictionary (Đã cập nhật) =2