如何使用 C# 中的 NameValueCollection 類?
NameValueCollection 為單個鍵設定多個值。現在讓我們看看如何在 C# 程式中使用它們。
設定集合 −
static NameValueCollection GetCollection() { NameValueCollection myCollection = new NameValueCollection(); myCollection.Add("Tim", "One"); myCollection.Add("John", "Two"); myCollection.Add("Jamie", "Three"); return myCollection; }
現在,使用 GetCollection() 方法使用 “AllKeys” 方法屬性來顯示所有鍵 −
NameValueCollection myCollection = GetCollection(); foreach (string key in myCollection.AllKeys) { Console.WriteLine(key); }
廣告