Computer >> Máy Tính >  >> Lập trình >> C#

Làm thế nào để sử dụng lớp NameValueCollection trong C #?

Các bộ NameValueCollection hơn một giá trị cho một chìa khóa duy nhất. Bây giờ chúng ta hãy xem làm thế nào để sử dụng chúng trong C # chương trình của chúng tôi.

Đặt bộ sưu tập -

static NameValueCollection GetCollection() {
   NameValueCollection myCollection = new NameValueCollection();
   myCollection.Add("Tim", "One");
   myCollection.Add("John", "Two");
   myCollection.Add("Jamie", "Three");
   return myCollection;
}

Bây giờ với phương pháp GetCollection () sử dụng các “AllKeys” phương pháp tài sản để hiển thị tất cả các phím -

NameValueCollection myCollection = GetCollection();
foreach (string key in myCollection.AllKeys) {
   Console.WriteLine(key);
}