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

Xóa tất cả các phần tử khỏi SortedList trong C #


Để xóa tất cả các phần tử khỏi Danh sách được sắp xếp, mã như sau -

Ví dụ

 using System; using System.Collections; public class Demo {public static void Main (String [] args) {SortedList sortedList =new SortedList (); sortedList.Add ("A", "1"); sortedList.Add ("B", "2"); sortedList.Add ("C", "3"); sortedList.Add ("D", "4"); sortedList.Add ("E", "5"); sortedList.Add ("F", "6"); sortedList.Add ("G", "7"); sortedList.Add ("H", "8"); sortedList.Add ("Tôi", "9"); sortedList.Add ("J", "10"); Console.WriteLine ("Các phần tử trong danh sách đã được sắp xếp ..."); foreach (DictionaryEntry d in sortedList) {Console.WriteLine ("Key =" + d.Key + ", Value =" + d.Value); } Console.WriteLine ("Số lượng cặp khóa-giá trị của SortedList =" + sortedList.Count); sortedList.Clear (); Console.WriteLine ("Số lượng SortedList (đã cập nhật) =" + sortedList.Count); }} 

Đầu ra

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

Các phần tử
 SortedList ... Key =A, Value =1Key =B, Value =2Key =C, Value =3Key =D, Value =4Key =E, Value =5Key =F, Value =6Key =G, Value =7Key =H, Value =8Key =I, Value =9Key =J, Value =10Count of SortedList key-value cặp =10Count of SortedList (đã cập nhật) =0 

Ví dụ

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

 using System; using System.Collections; public class Demo {public static void Main (String [] args) {SortedList sortedList =new SortedList (); sortedList.Add ("A", "1"); sortedList.Add ("B", "2"); sortedList.Add ("C", "3"); Console.WriteLine ("Số lượng cặp khóa-giá trị của SortedList =" + sortedList.Count); Console.WriteLine ("Các phần tử trong danh sách đã được sắp xếp ..."); foreach (DictionaryEntry d in sortedList) {Console.WriteLine ("Key =" + d.Key + ", Value =" + d.Value); } sortedList.Clear (); Console.WriteLine ("Số lượng cặp khóa-giá trị SortedList (đã cập nhật) =" + sortedList.Count); }} 

Đầu ra

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

 Số lượng cặp khóa-giá trị của Danh sách đã sắp xếp =3 phần tử Danh sách đã sắp xếp ... Khóa =A, Giá trị =1Key =B, Giá trị =2Key =C, Giá trị =3 Số lượng cặp khóa-giá trị của Danh sách được sắp xếp (đã cập nhật) =0