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

คุณสมบัติ Keys ของคลาส SortedList ใน C#

ดึงคีย์ใน SortedList ด้วยคุณสมบัติ Keys ก่อนตั้งค่า SortedList:

SortedList sl = new SortedList(); 
sl.Add("ST0", "One"); sl.Add("ST1", "Two"); sl.Add("ST2", "Three"); 
sl.Add("ST3", "Four"); sl.Add("ST4", "Five"); sl.Add("ST5", "Six"); sl.Add("ST6", "Seven");

ใช้ Keys property:

ICollection key = sl.Keys;

ตัวอย่าง

using System; 
using System.Collections; 
namespace Demo { 
    class Program { 
        static void Main(string[] args) { 
            SortedList sl = new SortedList(); 
            sl.Add("ST0", "One"); sl.Add("ST1", "Two"); sl.Add("ST2", "Three"); 
            sl.Add("ST3", "Four"); sl.Add("ST4", "Five"); sl.Add("ST5", "Six"); sl.Add("ST6", "Seven"); 
            ICollection key = sl.Keys; 
            foreach (string k in key) Console.WriteLine(k); 
        } 
    } 
}

ผลลัพธ์

ST0 ST1 ST2 ST3 ST4 ST5 ST6