---------- NameValueCollection 特性: 同一個key可重複Add(). 等於append! using System; using System.Collections; using System.Collections.Specialized; public class SamplesNameValueCollection { public static void Main() { // Creates and initializes a new NameValueCollection. NameValueCollection myCol = new NameValueCollection(); myCol.Add( "red", "rojo" ); myCol.Add( "green", "verde" ); myCol.Add( "blue", "azul" ); myCol.Add( "red", "rouge" ); // 同一個key可重複Add. 等於append! // Displays the values in the NameValueCollection in two different ways. Console.WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" ); PrintKeysAndValues( myCol ); Console.WriteLine( "Displays the elements using GetKey and Get:" ); PrintKeysAndValues2( myCol ); // Gets a value either by index or by key. Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] ); Console.WriteLine( "Key \"red\" has the value {0}.", myCol["red"] ); Console.WriteLine(); // Copies the values to a string array and displays the string array. String[] myStrArr = new String[myCol.Count]; myCol.CopyTo( myStrArr, 0 ); Console.WriteLine( "The string array contains:" ); foreach ( String s in myStrArr ) Console.WriteLine( " {0}", s ); Console.WriteLine(); // Searches for a key and deletes it. myCol.Remove( "green" ); Console.WriteLine( "The collection contains the following elements after removing \"green\":" ); PrintKeysAndValues( myCol ); // Clears the entire collection. myCol.Clear(); Console.WriteLine( "The collection contains the following elements after it is cleared:" ); PrintKeysAndValues( myCol ); } public static void PrintKeysAndValues( NameValueCollection myCol ) { IEnumerator myEnumerator = myCol.GetEnumerator(); Console.WriteLine( " KEY VALUE" ); foreach ( String s in myCol.AllKeys ) Console.WriteLine( " {0,-10} {1}", s, myCol[s] ); Console.WriteLine(); } public static void PrintKeysAndValues2( NameValueCollection myCol ) { Console.WriteLine( " [INDEX] KEY VALUE" ); for ( int i = 0; i < myCol.Count; i++ ) Console.WriteLine( " [{0}] {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) ); Console.WriteLine(); } } /* This code produces the following output. Displays the elements using the AllKeys property and the Item (indexer) property: KEY VALUE red rojo,rouge green verde blue azul Displays the elements using GetKey and Get: [INDEX] KEY VALUE [0] red rojo,rouge [1] green verde [2] blue azul Index 1 contains the value verde. Key "red" has the value rojo,rouge. The string array contains: rojo,rouge verde azul The collection contains the following elements after removing "green": KEY VALUE red rojo,rouge blue azul The collection contains the following elements after it is cleared: KEY VALUE */ ---------- 這個範例會從 Web.config 檔讀取索引鍵 customsetting1 所識別的應用程式設定。 appSettings 項目是字串的 NameValueCollection 集合。 使用集合項目會比使用其他組態項目要複雜一些。 System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null); if (0 < rootWebConfig1.AppSettings.Settings.Count) { System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings["customsetting1"]; if (null != customSetting) Console.WriteLine("customsetting1 application string = \"{0}\"", customSetting.Value); else Console.WriteLine("No customsetting1 application string"); } ---------- 表示相關 String 索引鍵和 String 值的集合,而這些可以利用索引鍵或索引來存取。 這個集合是以 NameObjectCollectionBase 類別為基礎。 不過,不同於 NameObjectCollectionBase,這個類別是在單一索引鍵下儲存多個字串值。 這個類別可用於標頭、查詢字串和表單資料。 每個元素都是索引鍵/值組。 NameValueCollection 的容量是 NameValueCollection 可以保存的項目數。 元素加入 NameValueCollection 時,容量會依所需透過重新配置自動增加。 雜湊程式碼提供者分配 NameValueCollection 中索引鍵的雜湊程式碼。 預設雜湊程式碼提供者為 CaseInsensitiveHashCodeProvider。 比較子判斷兩個索引鍵是否相等。預設比較子為 CaseInsensitiveComparer。