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

Thuộc tính Item của Hashtable trong C#

Item property get/set value với key chỉ định. Cũng dùng để thêm element mới.

Nếu key không tồn tại: myCollection["myNonexistentKey"] = myValue

Ví dụ

using System;
using System.Collections;
namespace Demo {
    class Program {
        static void Main(string[] args) {
            Hashtable ht = new Hashtable();
            ht.Add("One", "Amit"); ht.Add("Two", "Aman"); ht.Add("Three", "Raman");
            ht["Four"] = "David"; // Item property thêm mới
            ICollection key = ht.Keys;
            foreach (string k in key) Console.WriteLine(k + ": " + ht[k]);
            Console.ReadKey();
        }
    }
}

Kết quả

Three: Raman Four: David One: Amit Two: Aman