如何使用 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);
}

更新於: 2020 年 6 月 20 日

530 次瀏覽

開啟你的 事業

透過完成課程獲得認證

立即開始
廣告