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

Lấy các khóa trong SortedList C#

Giới thiệu

Thuộc tính Keys của SortedList trả về ICollection chứa tất cả khóa theo thứ tự đã sắp xếp. Cũng có GetKeyList() trả về IList view.

Ví dụ 1: Lấy tập khóa

using System;
using System.Collections;

public class Demo
{
    public static void Main(string[] args)
    {
        SortedList list = new SortedList();
        list.Add("One", "Finance");
        list.Add("Two", "Marketing");
        list.Add("Three", "Sales");
        list.Add("Four", "Purchase");
        list.Add("Five", "Operations");
        list.Add("Six", "IT");

        Console.WriteLine("Các phần tử SortedList...");
        foreach (DictionaryEntry d in list)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nChỉ số tại khóa 'One' = " + list.IndexOfKey("One"));
        ICollection col = list.Keys;
        Console.WriteLine("\nTập hợp các khóa...");
        foreach (string res in col)
            Console.WriteLine(res);
    }
}

Kết quả

Các phần tử SortedList...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Chỉ số tại khóa 'One' = 2
Tập hợp các khóa...
Five
Four
One
Six
Three
Two

Ví dụ 2: Hai SortedList, lấy khóa từng cái

using System;
using System.Collections;

public class Demo
{
    public static void Main(string[] args)
    {
        SortedList list1 = new SortedList();
        list1.Add("One", 1);
        list1.Add("Two ", 2);
        list1.Add("Three ", 3);
        list1.Add("Four", 4);
        list1.Add("Five", 5);
        list1.Add("Six", 6);
        list1.Add("Seven ", 7);
        list1.Add("Eight ", 8);
        list1.Add("Nine", 9);
        list1.Add("Ten", 10);

        Console.WriteLine("Các phần tử SortedList1...");
        foreach (DictionaryEntry d in list1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        ICollection col = list1.Keys;
        Console.WriteLine("\nTập khóa trong List1...");
        foreach (string res in col)
            Console.WriteLine(res);

        SortedList list2 = new SortedList();
        list2.Add("A", "Accessories");
        list2.Add("B", "Books");
        list2.Add("C", "Smart Wearable Tech");
        list2.Add("D", "Home Appliances");

        Console.WriteLine("\nCác phần tử SortedList2...");
        foreach (DictionaryEntry d in list2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        col = list2.Keys;
        Console.WriteLine("\nTập khóa trong List2...");
        foreach (string res in col)
            Console.WriteLine(res);
    }
}

Kết quả

Các phần tử SortedList1...
Eight  8
Five 5
Four 4
Nine 9
One 1
Seven  7
Six 6
Ten 10
Three  3
Two  2
Tập khóa trong List1...
Eight
Five
Four
Nine
One
Seven
Six
Ten
Three
Two
Các phần tử SortedList2...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Tập khóa trong List2...
A
B
C
D

Mẹo

  • Khóa được sắp xếp tự động (theo IComparable hoặc comparer tùy chỉnh).
  • Dùng Values để lấy tập giá trị.
  • Với generic SortedList<TKey, TValue>, Keys trả về IList<TKey>.