mscorlib(4.0.0.0) API with additions
NameValueCollection.cs
3 using System.Text;
4 
6 {
9  [global::__DynamicallyInvokable]
11  {
12  private string[] _all;
13 
14  private string[] _allKeys;
15 
20  public string this[string name]
21  {
22  [global::__DynamicallyInvokable]
23  get
24  {
25  return Get(name);
26  }
27  [global::__DynamicallyInvokable]
28  set
29  {
30  Set(name, value);
31  }
32  }
33 
39  public string this[int index]
40  {
41  get
42  {
43  return Get(index);
44  }
45  }
46 
49  public virtual string[] AllKeys
50  {
51  get
52  {
53  if (_allKeys == null)
54  {
55  _allKeys = BaseGetAllKeys();
56  }
57  return _allKeys;
58  }
59  }
60 
63  {
64  }
65 
71  : base(col?.Comparer)
72  {
73  Add(col);
74  }
75 
79  [Obsolete("Please use NameValueCollection(IEqualityComparer) instead.")]
80  public NameValueCollection(IHashCodeProvider hashProvider, IComparer comparer)
81  : base(hashProvider, comparer)
82  {
83  }
84 
89  public NameValueCollection(int capacity)
90  : base(capacity)
91  {
92  }
93 
96  public NameValueCollection(IEqualityComparer equalityComparer)
97  : base(equalityComparer)
98  {
99  }
100 
106  public NameValueCollection(int capacity, IEqualityComparer equalityComparer)
107  : base(capacity, equalityComparer)
108  {
109  }
110 
118  public NameValueCollection(int capacity, NameValueCollection col)
119  : base(capacity, col?.Comparer)
120  {
121  if (col == null)
122  {
123  throw new ArgumentNullException("col");
124  }
125  base.Comparer = col.Comparer;
126  Add(col);
127  }
128 
135  [Obsolete("Please use NameValueCollection(Int32, IEqualityComparer) instead.")]
136  public NameValueCollection(int capacity, IHashCodeProvider hashProvider, IComparer comparer)
137  : base(capacity, hashProvider, comparer)
138  {
139  }
140 
141  internal NameValueCollection(DBNull dummy)
142  : base(dummy)
143  {
144  }
145 
150  : base(info, context)
151  {
152  }
153 
155  protected void InvalidateCachedArrays()
156  {
157  _all = null;
158  _allKeys = null;
159  }
160 
161  private static string GetAsOneString(ArrayList list)
162  {
163  int num = list?.Count ?? 0;
164  if (num == 1)
165  {
166  return (string)list[0];
167  }
168  if (num > 1)
169  {
170  StringBuilder stringBuilder = new StringBuilder((string)list[0]);
171  for (int i = 1; i < num; i++)
172  {
173  stringBuilder.Append(',');
174  stringBuilder.Append((string)list[i]);
175  }
176  return stringBuilder.ToString();
177  }
178  return null;
179  }
180 
181  private static string[] GetAsStringArray(ArrayList list)
182  {
183  int num = list?.Count ?? 0;
184  if (num == 0)
185  {
186  return null;
187  }
188  string[] array = new string[num];
189  list.CopyTo(0, array, 0, num);
190  return array;
191  }
192 
198  public void Add(NameValueCollection c)
199  {
200  if (c == null)
201  {
202  throw new ArgumentNullException("c");
203  }
205  int count = c.Count;
206  for (int i = 0; i < count; i++)
207  {
208  string key = c.GetKey(i);
209  string[] values = c.GetValues(i);
210  if (values != null)
211  {
212  for (int j = 0; j < values.Length; j++)
213  {
214  Add(key, values[j]);
215  }
216  }
217  else
218  {
219  Add(key, null);
220  }
221  }
222  }
223 
226  public virtual void Clear()
227  {
228  if (base.IsReadOnly)
229  {
230  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
231  }
233  BaseClear();
234  }
235 
246  public void CopyTo(Array dest, int index)
247  {
248  if (dest == null)
249  {
250  throw new ArgumentNullException("dest");
251  }
252  if (dest.Rank != 1)
253  {
254  throw new ArgumentException(SR.GetString("Arg_MultiRank"));
255  }
256  if (index < 0)
257  {
258  throw new ArgumentOutOfRangeException("index", SR.GetString("IndexOutOfRange", index.ToString(CultureInfo.CurrentCulture)));
259  }
260  if (dest.Length - index < Count)
261  {
262  throw new ArgumentException(SR.GetString("Arg_InsufficientSpace"));
263  }
264  int count = Count;
265  if (_all == null)
266  {
267  string[] array = new string[count];
268  for (int i = 0; i < count; i++)
269  {
270  array[i] = Get(i);
271  dest.SetValue(array[i], i + index);
272  }
273  _all = array;
274  }
275  else
276  {
277  for (int j = 0; j < count; j++)
278  {
279  dest.SetValue(_all[j], j + index);
280  }
281  }
282  }
283 
287  public bool HasKeys()
288  {
289  return InternalHasKeys();
290  }
291 
292  internal virtual bool InternalHasKeys()
293  {
294  return BaseHasKeys();
295  }
296 
301  public virtual void Add(string name, string value)
302  {
303  if (base.IsReadOnly)
304  {
305  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
306  }
308  ArrayList arrayList = (ArrayList)BaseGet(name);
309  if (arrayList == null)
310  {
311  arrayList = new ArrayList(1);
312  if (value != null)
313  {
314  arrayList.Add(value);
315  }
316  BaseAdd(name, arrayList);
317  }
318  else if (value != null)
319  {
320  arrayList.Add(value);
321  }
322  }
323 
327  public virtual string Get(string name)
328  {
329  ArrayList list = (ArrayList)BaseGet(name);
330  return GetAsOneString(list);
331  }
332 
336  public virtual string[] GetValues(string name)
337  {
338  ArrayList list = (ArrayList)BaseGet(name);
339  return GetAsStringArray(list);
340  }
341 
346  public virtual void Set(string name, string value)
347  {
348  if (base.IsReadOnly)
349  {
350  throw new NotSupportedException(SR.GetString("CollectionReadOnly"));
351  }
353  ArrayList arrayList = new ArrayList(1);
354  arrayList.Add(value);
355  BaseSet(name, arrayList);
356  }
357 
361  public virtual void Remove(string name)
362  {
364  BaseRemove(name);
365  }
366 
372  public virtual string Get(int index)
373  {
374  ArrayList list = (ArrayList)BaseGet(index);
375  return GetAsOneString(list);
376  }
377 
383  public virtual string[] GetValues(int index)
384  {
385  ArrayList list = (ArrayList)BaseGet(index);
386  return GetAsStringArray(list);
387  }
388 
394  public virtual string GetKey(int index)
395  {
396  return BaseGetKey(index);
397  }
398  }
399 }
Supplies a hash code for an object, using a custom hash function.
virtual string [] GetValues(int index)
Gets the values at the specified index of the T:System.Collections.Specialized.NameValueCollection.
virtual string Get(string name)
Gets the values associated with the specified key from the T:System.Collections.Specialized....
Compares two objects for equivalence, where string comparisons are case-sensitive.
Definition: Comparer.cs:11
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents a collection of associated T:System.String keys and T:System.String values that can be acc...
virtual void Clear()
Invalidates the cached arrays and removes all entries from the T:System.Collections....
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
NameValueCollection(int capacity)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
virtual void Add(string name, string value)
Adds an entry with the specified name and value to the T:System.Collections.Specialized....
bool HasKeys()
Gets a value indicating whether the T:System.Collections.Specialized.NameValueCollection contains key...
virtual string Get(int index)
Gets the values at the specified index of the T:System.Collections.Specialized.NameValueCollection co...
virtual string [] AllKeys
Gets all the keys in the T:System.Collections.Specialized.NameValueCollection.
virtual void Remove(string name)
Removes the entries with the specified key from the T:System.Collections.Specialized....
NameValueCollection(IEqualityComparer equalityComparer)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
Definition: __Canon.cs:3
NameValueCollection(int capacity, IHashCodeProvider hashProvider, IComparer comparer)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Describes the source and destination of a given serialized stream, and provides an additional caller-...
int Rank
Gets the rank (number of dimensions) of the T:System.Array. For example, a one-dimensional array retu...
Definition: Array.cs:852
void BaseClear()
Removes all entries from the T:System.Collections.Specialized.NameObjectCollectionBase instance.
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
virtual string [] GetValues(string name)
Gets the values associated with the specified key from the T:System.Collections.Specialized....
NameValueCollection(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
string [] BaseGetAllKeys()
Returns a T:System.String array that contains all the keys in the T:System.Collections....
unsafe void SetValue(object value, int index)
Sets a value to the element at the specified position in the one-dimensional T:System....
Definition: Array.cs:1625
Defines methods to support the comparison of objects for equality.
virtual string GetKey(int index)
Gets the key at the specified index of the T:System.Collections.Specialized.NameValueCollection.
virtual void Set(string name, string value)
Sets the value of an entry in the T:System.Collections.Specialized.NameValueCollection.
Exposes a method that compares two objects.
Definition: IComparer.cs:8
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
string BaseGetKey(int index)
Gets the key of the entry at the specified index of the T:System.Collections.Specialized....
Provides the abstract base class for a collection of associated T:System.String keys and T:System....
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
void CopyTo(Array dest, int index)
Copies the entire T:System.Collections.Specialized.NameValueCollection to a compatible one-dimensiona...
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
NameValueCollection(IHashCodeProvider hashProvider, IComparer comparer)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
void InvalidateCachedArrays()
Resets the cached arrays of the collection to null.
object BaseGet(string name)
Gets the value of the first entry with the specified key from the T:System.Collections....
int Length
Gets the total number of elements in all the dimensions of the T:System.Array.
Definition: Array.cs:829
Specifies that the class can be serialized.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Specialized....
void BaseSet(string name, object value)
Sets the value of the first entry with the specified key in the T:System.Collections....
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
void Add(NameValueCollection c)
Copies the entries in the specified T:System.Collections.Specialized.NameValueCollection to the curre...
bool BaseHasKeys()
Gets a value indicating whether the T:System.Collections.Specialized.NameObjectCollectionBase instanc...
void BaseRemove(string name)
Removes the entries with the specified key from the T:System.Collections.Specialized....
void BaseAdd(string name, object value)
Adds an entry with the specified key and value into the T:System.Collections.Specialized....
NameValueCollection()
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...
Represents a nonexistent value. This class cannot be inherited.
Definition: DBNull.cs:10
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
NameValueCollection(int capacity, IEqualityComparer equalityComparer)
Initializes a new instance of the T:System.Collections.Specialized.NameValueCollection class that is ...