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

Thuộc tính SortedDictionary.Values trong C#

Thuộc tính SortedDictionary<TKey, TValue>.Values trong C# được dùng để lấy một tập hợp (collection) chứa các giá trị (values) trong SortedDictionary. Tập hợp trả về là kiểu SortedDictionary<TKey, TValue>.ValueCollection.

Cú pháp

public System.Collections.Generic.SortedDictionary.ValueCollection Values { get; }

Ví dụ 1: Liệt kê Key-Value và lấy danh sách Values

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "Ultrabook");
        sortedDict.Add(2, "Alienware");
        sortedDict.Add(3, "Notebook");
        sortedDict.Add(4, "Connector");
        sortedDict.Add(5, "Flash Drive");
        sortedDict.Add(6, "SSD");
        sortedDict.Add(7, "HDD");
        sortedDict.Add(8, "Earphone");

        Console.WriteLine("Các cặp key-value trong SortedDictionary...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        Console.WriteLine("\nXóa key 7: " + sortedDict.Remove(7));

        Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        // Lấy tập hợp Keys
        SortedDictionary.KeyCollection keyList = sortedDict.Keys;
        Console.WriteLine("\nDanh sách Keys...");
        foreach (int i in keyList)
        {
            Console.WriteLine(i);
        }

        // Lấy tập hợp Values
        SortedDictionary.ValueCollection valList = sortedDict.Values;
        Console.WriteLine("\nDanh sách Values...");
        foreach (string s in valList)
        {
            Console.WriteLine(s);
        }
    }
}

Kết quả

Các cặp key-value trong SortedDictionary...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 7, Value = HDD
Key = 8, Value = Earphone

Xóa key 7: True

Các cặp key-value SAU KHI CẬP NHẬT...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 8, Value = Earphone

Danh sách Keys...
1
2
3
4
5
6
8

Danh sách Values...
Ultrabook
Alienware
Notebook
Connector
Flash Drive
SSD
Earphone

Ví dụ 2: SortedDictionary với key int, value string (loại xe)

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "SUV");
        sortedDict.Add(2, "MUV");
        sortedDict.Add(3, "Hatchback");
        sortedDict.Add(4, "AUV");
        sortedDict.Add(5, "Convertible");
        sortedDict.Add(6, "Crossover");
        sortedDict.Add(7, "Utility Vehicle");

        Console.WriteLine("Các cặp key-value...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        Console.WriteLine("\nXóa key 2: " + sortedDict.Remove(2));

        Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        SortedDictionary.KeyCollection keyList = sortedDict.Keys;
        Console.WriteLine("\nDanh sách Keys...");
        foreach (int i in keyList) Console.WriteLine(i);

        SortedDictionary.ValueCollection valList = sortedDict.Values;
        Console.WriteLine("\nDanh sách Values...");
        foreach (string s in valList) Console.WriteLine(s);
    }
}

Kết quả

Các cặp key-value...
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Hatchback
Key = 4, Value = AUV
Key = 5, Value = Convertible
Key = 6, Value = Crossover
Key = 7, Value = Utility Vehicle

Xóa key 2: True

Các cặp key-value SAU KHI CẬP NHẬT...
Key = 1, Value = SUV
Key = 3, Value = Hatchback
Key = 4, Value = AUV
Key = 5, Value = Convertible
Key = 6, Value = Crossover
Key = 7, Value = Utility Vehicle

Danh sách Keys...
1
3
4
5
6
7

Danh sách Values...
SUV
Hatchback
AUV
Convertible
Crossover
Utility Vehicle

Đặc điểm của ValueCollection

  • Thứ tự các value tương ứng với thứ tự key đã sắp xếp (tăng dần).
  • Là view động: thay đổi SortedDictionary gốc sẽ phản ánh ngay trong ValueCollection.
  • Hỗ trợ foreach, CopyTo, Count.
  • Không hỗ trợ truy cập bằng chỉ mục [index] (dùng ElementAt của LINQ nếu cần).

Chuyển sang List/Array nếu cần

// Chuyển sang List
List valueList = sortedDict.Values.ToList();

// Chuyển sang Array
string[] valueArray = sortedDict.Values.ToArray();