2 // System.Collections.Specialized.NameValueCollection.cs
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2011 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.Serialization;
35 namespace System.Collections.Specialized{
38 public class NameValueCollection : NameObjectCollectionBase
40 string[] cachedAllKeys = null;
41 string[] cachedAll = null;
43 //--------------------- Constructors -----------------------------
45 public NameValueCollection () : base ()
49 public NameValueCollection (int capacity) : base (capacity)
53 public NameValueCollection (NameValueCollection col) : base (( col == null ) ? null : col.EqualityComparer ,
54 (col == null) ? null : col.Comparer,
55 (col == null) ? null : col.HashCodeProvider)
58 throw new ArgumentNullException ("col");
62 [Obsolete ("Use NameValueCollection (IEqualityComparer)")]
63 public NameValueCollection (IHashCodeProvider hashProvider, IComparer comparer)
64 : base (hashProvider, comparer)
69 public NameValueCollection (int capacity, NameValueCollection col)
70 : base (capacity, (col == null) ? null : col.HashCodeProvider,
71 (col == null) ? null : col.Comparer)
76 protected NameValueCollection (SerializationInfo info, StreamingContext context)
82 [Obsolete ("Use NameValueCollection (IEqualityComparer)")]
83 public NameValueCollection (int capacity, IHashCodeProvider hashProvider, IComparer comparer)
84 :base (capacity, hashProvider, comparer)
89 public NameValueCollection (IEqualityComparer equalityComparer)
90 : base (equalityComparer)
94 public NameValueCollection (int capacity, IEqualityComparer equalityComparer)
95 : base (capacity, equalityComparer)
99 public virtual string[] AllKeys
102 if (cachedAllKeys == null)
103 cachedAllKeys = BaseGetAllKeys ();
104 return this.cachedAllKeys;
108 public string this [int index]
111 return this.Get (index);
115 public string this [string name] {
117 return this.Get (name);
120 this.Set (name,value);
124 public void Add (NameValueCollection c)
127 throw new NotSupportedException ("Collection is read-only");
129 throw new ArgumentNullException ("c");
131 // make sense - but it's not the exception thrown
132 // throw new ArgumentNullException ();
134 InvalidateCachedArrays ();
136 for (int i=0; i < max; i++){
137 string key = c.GetKey (i);
138 string[] values = c.GetValues (i);
140 if (values != null && values.Length > 0) {
141 foreach (string value in values)
148 /// in SDK doc: If the same value already exists under the same key in the collection,
149 /// it just adds one more value in other words after
151 /// NameValueCollection nvc;
152 /// nvc.Add ("LAZY","BASTARD")
153 /// nvc.Add ("LAZY","BASTARD")
155 /// nvc.Get ("LAZY") will be "BASTARD,BASTARD" instead of "BASTARD"
157 public virtual void Add (string name, string value)
160 throw new NotSupportedException ("Collection is read-only");
162 InvalidateCachedArrays ();
163 ArrayList values = (ArrayList)BaseGet (name);
165 values = new ArrayList ();
168 BaseAdd (name, values);
177 public virtual void Clear ()
180 throw new NotSupportedException ("Collection is read-only");
181 InvalidateCachedArrays ();
185 public void CopyTo (Array dest, int index)
188 throw new ArgumentNullException ("dest", "Null argument - dest");
190 throw new ArgumentOutOfRangeException ("index", "index is less than 0");
192 throw new ArgumentException ("dest", "multidim");
194 if (cachedAll == null)
197 cachedAll.CopyTo (dest, index);
198 } catch (ArrayTypeMismatchException) {
199 throw new InvalidCastException();
203 private void RefreshCachedAll ()
205 this.cachedAll = null;
206 int max = this.Count;
207 cachedAll = new string [max];
208 for (int i = 0; i < max; i++)
209 cachedAll [i] = this.Get (i);
212 public virtual string Get (int index)
214 ArrayList values = (ArrayList)BaseGet (index);
215 // if index is out of range BaseGet throws an ArgumentOutOfRangeException
217 return AsSingleString (values);
220 public virtual string Get (string name)
222 ArrayList values = (ArrayList)BaseGet (name);
223 return AsSingleString (values);
226 private static string AsSingleString (ArrayList values)
228 const char separator = ',';
232 int max = values.Count;
238 return (string)values [0];
240 return String.Concat ((string)values [0], separator, (string)values [1]);
243 for (int i = 0; i < max; i++)
244 len += ((string)values [i]).Length;
245 StringBuilder sb = new StringBuilder ((string)values [0], len);
246 for (int i = 1; i < max; i++){
247 sb.Append (separator);
248 sb.Append (values [i]);
251 return sb.ToString ();
256 public virtual string GetKey (int index)
258 return BaseGetKey (index);
262 public virtual string[] GetValues (int index)
264 ArrayList values = (ArrayList)BaseGet (index);
266 return AsStringArray (values);
270 public virtual string[] GetValues (string name)
272 ArrayList values = (ArrayList)BaseGet (name);
274 return AsStringArray (values);
277 private static string[] AsStringArray (ArrayList values)
281 int max = values.Count;//get_Count ();
285 string[] valArray = new string[max];
286 values.CopyTo (valArray);
290 public bool HasKeys ()
292 return BaseHasKeys ();
295 public virtual void Remove (string name)
298 throw new NotSupportedException ("Collection is read-only");
299 InvalidateCachedArrays ();
304 public virtual void Set (string name, string value)
307 throw new NotSupportedException ("Collection is read-only");
309 InvalidateCachedArrays ();
311 ArrayList values = new ArrayList ();
314 BaseSet (name,values);
317 // remove all entries
318 BaseSet (name, null);
322 protected void InvalidateCachedArrays ()
324 cachedAllKeys = null;