13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Generic collection classes <strong>in</strong> .NET 2.099calls. To make those operations thread-safe, the collection needs to be locked for theduration of the operation. (It requires cooperation from other code us<strong>in</strong>g the samecollection, of course.) In short, the Synchronized method gave the appearance ofsafety without the reality. It’s better not to give the wrong impression <strong>in</strong> the firstplace—developers just have to be careful when work<strong>in</strong>g with collections accessed <strong>in</strong>multiple threads. SynchronizedCollection performs broadly the same role as asynchronized ArrayList. I would argue that it’s still not a good idea to use this, for thereasons outl<strong>in</strong>ed <strong>in</strong> this paragraph—the safety provided is largely illusory. Ironically,this would be a great collection to support a ForEach method, where it could automaticallyhold the lock for the duration of the iteration over the collection—but there’sno such method.That completes our coverage of List. The next collection under the microscopeis Dictionary, which we’ve already seen so much of.3.5.2 DictionaryThere is less to say about Dictionary (just called Dictionary forthe rest of this section, for simplicity) than there was about List, although it’sanother heavily used type. As stated earlier, it’s the generic replacement for Hashtableand the related classes, such as Str<strong>in</strong>gDictionary. There aren’t many features present<strong>in</strong> Dictionary that aren’t <strong>in</strong> Hashtable, although this is partly because the ability tospecify a comparison <strong>in</strong> the form of an IEqualityComparer was added to Hashtable <strong>in</strong>.NET 2.0. This allows for th<strong>in</strong>gs like case-<strong>in</strong>sensitive comparisons of str<strong>in</strong>gs withoutus<strong>in</strong>g a separate type of dictionary. IEqualityComparer and its generic equivalent,IEqualityComparer, have both Equals and GetHashCode. Prior to .NET 2.0 thesewere split <strong>in</strong>to IComparer (which had to give an order<strong>in</strong>g, not just test for equality) andIHashCodeProvider. This separation was awkward, hence the move to IEquality-Comparer for 2.0. Dictionary exposes its IEqualityComparer <strong>in</strong> the publicComparer property.The most important difference between Dictionary and Hashtable (beyond thenormal benefits of generics) is their behavior when asked to fetch the value associatedwith a key that they don’t know about. When presented with a key that isn’t <strong>in</strong> themap, the <strong>in</strong>dexer of Hashtable will just return null. By contrast, Dictionary willthrow a KeyNotFoundException. Both of them support the Conta<strong>in</strong>sKey method totell beforehand whether a given key is present. Dictionary also providesTryGetValue, which retrieves the value if a suitable entry is present, stor<strong>in</strong>g it <strong>in</strong> theoutput parameter and return<strong>in</strong>g true. If the key is not present, TryGetValue will setthe output parameter to the default value of TValue and return false. This avoidshav<strong>in</strong>g to search for the key twice, while still allow<strong>in</strong>g the caller to dist<strong>in</strong>guish betweenthe situation where a key isn’t present at all, and the one where it’s present but its associatedvalue is the default value of TValue. Mak<strong>in</strong>g the <strong>in</strong>dexer throw an exception isof more debatable merit, but it does make it very clear when a lookup has failed<strong>in</strong>stead of mask<strong>in</strong>g the failure by return<strong>in</strong>g a potentially valid value.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!