1 //------------------------------------------------------------------------------
2 // <copyright file="StringDictionary.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
9 namespace System.Collections.Specialized {
10 using System.Runtime.InteropServices;
11 using System.Diagnostics;
13 using System.Collections;
14 using System.ComponentModel.Design.Serialization;
15 using System.Globalization;
18 /// <para>Implements a hashtable with the key strongly typed to be
19 /// a string rather than an object. </para>
20 /// <para>Consider this class obsolete - use Dictionary<String, String> instead
21 /// with a proper StringComparer instance.</para>
24 [DesignerSerializer("System.Diagnostics.Design.StringDictionaryCodeDomSerializer, " + AssemblyRef.SystemDesign, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + AssemblyRef.SystemDesign)]
26 public class StringDictionary : IEnumerable {
28 // For compatibility, we want the Keys property to return values in lower-case.
29 // That means using ToLower in each property on this type. Also for backwards
30 // compatibility, we will be converting strings to lower-case, which has a
31 // problem for some Georgian alphabets. Can't really fix it now though...
32 internal Hashtable contents = new Hashtable();
36 /// <para>Initializes a new instance of the StringDictionary class.</para>
37 /// <para>If you're using file names, registry keys, etc, you want to use
38 /// a Dictionary<String, Object> and use
39 /// StringComparer.OrdinalIgnoreCase.</para>
41 public StringDictionary() {
45 /// <para>Gets the number of key-and-value pairs in the StringDictionary.</para>
47 public virtual int Count {
49 return contents.Count;
55 /// <para>Indicates whether access to the StringDictionary is synchronized (thread-safe). This property is
58 public virtual bool IsSynchronized {
60 return contents.IsSynchronized;
65 /// <para>Gets or sets the value associated with the specified key.</para>
67 public virtual string this[string key] {
70 throw new ArgumentNullException("key");
73 return (string) contents[key.ToLower(CultureInfo.InvariantCulture)];
77 throw new ArgumentNullException("key");
80 contents[key.ToLower(CultureInfo.InvariantCulture)] = value;
85 /// <para>Gets a collection of keys in the StringDictionary.</para>
87 public virtual ICollection Keys {
95 /// <para>Gets an object that can be used to synchronize access to the StringDictionary.</para>
97 public virtual object SyncRoot {
99 return contents.SyncRoot;
104 /// <para>Gets a collection of values in the StringDictionary.</para>
106 public virtual ICollection Values {
108 return contents.Values;
113 /// <para>Adds an entry with the specified key and value into the StringDictionary.</para>
115 public virtual void Add(string key, string value) {
117 throw new ArgumentNullException("key");
120 contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
124 /// <para>Removes all entries from the StringDictionary.</para>
126 public virtual void Clear() {
131 /// <para>Determines if the string dictionary contains a specific key</para>
133 public virtual bool ContainsKey(string key) {
135 throw new ArgumentNullException("key");
138 return contents.ContainsKey(key.ToLower(CultureInfo.InvariantCulture));
142 /// <para>Determines if the StringDictionary contains a specific value.</para>
144 public virtual bool ContainsValue(string value) {
145 return contents.ContainsValue(value);
149 /// <para>Copies the string dictionary values to a one-dimensional <see cref='System.Array'/> instance at the
150 /// specified index.</para>
152 public virtual void CopyTo(Array array, int index) {
153 contents.CopyTo(array, index);
157 /// <para>Returns an enumerator that can iterate through the string dictionary.</para>
159 public virtual IEnumerator GetEnumerator() {
160 return contents.GetEnumerator();
164 /// <para>Removes the entry with the specified key from the string dictionary.</para>
166 public virtual void Remove(string key) {
168 throw new ArgumentNullException("key");
171 contents.Remove(key.ToLower(CultureInfo.InvariantCulture));
175 /// Make this StringDictionary subservient to some other collection.
176 /// <para>Some code was replacing the contents field with a Hashtable created elsewhere.
177 /// While it may not have been incorrect, we don't want to encourage that pattern, because
178 /// it will replace the IEqualityComparer in the Hashtable, and creates a possibly-undesirable
179 /// link between two seemingly different collections. Let's discourage that somewhat by
180 /// making this an explicit method call instead of an internal field assignment.</para>
182 /// <param name="useThisHashtableInstead">Replaces the backing store with another, possibly aliased Hashtable.</param>
183 internal void ReplaceHashtable(Hashtable useThisHashtableInstead) {
184 contents = useThisHashtableInstead;