Initial commit
[mono.git] / mcs / class / referencesource / System / compmod / system / collections / specialized / stringdictionary.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="StringDictionary.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9 namespace System.Collections.Specialized {
10     using System.Runtime.InteropServices;
11     using System.Diagnostics;
12     using System;
13     using System.Collections;
14     using System.ComponentModel.Design.Serialization;
15     using System.Globalization;
16     
17     /// <devdoc>
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&lt;String, String&gt; instead
21     ///       with a proper StringComparer instance.</para>
22     /// </devdoc>
23     [Serializable]
24     [DesignerSerializer("System.Diagnostics.Design.StringDictionaryCodeDomSerializer, " + AssemblyRef.SystemDesign, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + AssemblyRef.SystemDesign)]
25     // @
26     public class StringDictionary : IEnumerable {
27
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();
33
34         
35         /// <devdoc>
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&lt;String, Object&gt; and use 
39         /// StringComparer.OrdinalIgnoreCase.</para>
40         /// </devdoc>
41         public StringDictionary() {
42         }
43
44         /// <devdoc>
45         /// <para>Gets the number of key-and-value pairs in the StringDictionary.</para>
46         /// </devdoc>
47         public virtual int Count {
48             get {
49                 return contents.Count;
50             }
51         }
52
53         
54         /// <devdoc>
55         /// <para>Indicates whether access to the StringDictionary is synchronized (thread-safe). This property is 
56         ///    read-only.</para>
57         /// </devdoc>
58         public virtual bool IsSynchronized {
59             get {
60                 return contents.IsSynchronized;
61             }
62         }
63
64         /// <devdoc>
65         ///    <para>Gets or sets the value associated with the specified key.</para>
66         /// </devdoc>
67         public virtual string this[string key] {
68             get {
69                 if( key == null ) {
70                     throw new ArgumentNullException("key");
71                 }
72
73                 return (string) contents[key.ToLower(CultureInfo.InvariantCulture)];
74             }
75             set {
76                 if( key == null ) {
77                     throw new ArgumentNullException("key");
78                 }
79
80                 contents[key.ToLower(CultureInfo.InvariantCulture)] = value;
81             }
82         }
83
84         /// <devdoc>
85         /// <para>Gets a collection of keys in the StringDictionary.</para>
86         /// </devdoc>
87         public virtual ICollection Keys {
88             get {
89                 return contents.Keys;
90             }
91         }
92
93         
94         /// <devdoc>
95         /// <para>Gets an object that can be used to synchronize access to the StringDictionary.</para>
96         /// </devdoc>
97         public virtual object SyncRoot {
98             get {
99                 return contents.SyncRoot;
100             }
101         }
102
103         /// <devdoc>
104         /// <para>Gets a collection of values in the StringDictionary.</para>
105         /// </devdoc>
106         public virtual ICollection Values {
107             get {
108                 return contents.Values;
109             }
110         }
111
112         /// <devdoc>
113         /// <para>Adds an entry with the specified key and value into the StringDictionary.</para>
114         /// </devdoc>
115         public virtual void Add(string key, string value) {
116             if( key == null ) {
117                 throw new ArgumentNullException("key");
118             }
119
120             contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
121         }
122
123         /// <devdoc>
124         /// <para>Removes all entries from the StringDictionary.</para>
125         /// </devdoc>
126         public virtual void Clear() {
127             contents.Clear();
128         }
129
130         /// <devdoc>
131         ///    <para>Determines if the string dictionary contains a specific key</para>
132         /// </devdoc>
133         public virtual bool ContainsKey(string key) {
134             if( key == null ) {
135                 throw new ArgumentNullException("key");
136             }
137
138             return contents.ContainsKey(key.ToLower(CultureInfo.InvariantCulture));
139         }
140
141         /// <devdoc>
142         /// <para>Determines if the StringDictionary contains a specific value.</para>
143         /// </devdoc>
144         public virtual bool ContainsValue(string value) {
145             return contents.ContainsValue(value);
146         }
147
148         /// <devdoc>
149         /// <para>Copies the string dictionary values to a one-dimensional <see cref='System.Array'/> instance at the 
150         ///    specified index.</para>
151         /// </devdoc>
152         public virtual void CopyTo(Array array, int index) {
153             contents.CopyTo(array, index);
154         }
155
156         /// <devdoc>
157         ///    <para>Returns an enumerator that can iterate through the string dictionary.</para>
158         /// </devdoc>
159         public virtual IEnumerator GetEnumerator() {
160             return contents.GetEnumerator();
161         }
162
163         /// <devdoc>
164         ///    <para>Removes the entry with the specified key from the string dictionary.</para>
165         /// </devdoc>
166         public virtual void Remove(string key) {
167             if( key == null ) {
168                 throw new ArgumentNullException("key");
169             }
170
171             contents.Remove(key.ToLower(CultureInfo.InvariantCulture));
172         }
173
174         /// <summary>
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>
181         /// </summary>
182         /// <param name="useThisHashtableInstead">Replaces the backing store with another, possibly aliased Hashtable.</param>
183         internal void ReplaceHashtable(Hashtable useThisHashtableInstead) {
184             contents = useThisHashtableInstead;
185         }
186     }
187
188 }