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

Lấy danh sách các Giá trị của một đối tượng SortedList trong C #

Để nhận danh sách các giá trị của một đối tượng SortedList, mã như sau -

Ví dụ

 using System; using System.Collections; public class Demo {public static void Main (String [] args) {SortedList list =new SortedList (); list.Add ("A", 1); list.Add ("B", 2); list.Add ("C", 3); list.Add ("D", 4); list.Add ("E", 5); list.Add ("F", 6); list.Add ("G", 7); list.Add ("H", 8); Console.WriteLine ("Các phần tử trong danh sách đã được sắp xếp ..."); foreach (DictionaryEntry d trong danh sách) {Console.WriteLine (d.Key + "" + d.Value); } Console.WriteLine ("\ nDanh sách các giá trị ... SortedList"); IList col =list.GetValueList (); foreach (int res in col) Console.WriteLine (res); }} 

Đầu ra

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

 Các phần tử SortedList ... A 1B 2C 3D 4E 5F 6G 7H 8Danh sách các giá trị ... SortedList12345678 

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 list =new SortedList (); list.Add ("Một", "IT"); list.Add ("Hai", "Thao tác"); list.Add ("Ba", "Tiếp thị"); list.Add ("Bốn", "Mua"); list.Add ("Năm", "Bán hàng"); list.Add ("Sáu", "Tài chính"); Console.WriteLine ("Các phần tử trong danh sách đã được sắp xếp ..."); foreach (DictionaryEntry d trong danh sách) {Console.WriteLine (d.Key + "" + d.Value); } Console.WriteLine ("\ nDanh sách các giá trị ... SortedList"); IList col =list.GetValueList (); foreach (string res in col) Console.WriteLine (res); }} 

Đầu ra

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

 Các yếu tố trong danh sách đã sắp xếp ... Năm lần bán hàngMua hàng bốn lầnMột ITSix Tài chínhBa lần tiếp thịHai hoạt độngDanh sách giá trị ...