Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Thuộc tính Keys của SortedList trong C#

Lấy keys trong SortedList bằng Keys property.

Khởi tạo 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");

Lấy keys:

ICollection key = sl.Keys;

Ví dụ

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);
        }
    }
}

Kết quả

ST0 ST1 ST2 ST3 ST4 ST5 ST6