Example 9 1 looking up an entry in a dictionary

Info icon This preview shows pages 323–325. Sign up to view the full content.

Example 9-1. Looking up an entry in a dictionary string definition = myDictionary["sea"]; Just as printed dictionaries vary in what you get when you look up a word, so can .NET dictionaries. The Dictionary type in the System.Collections.Generic namespace is a generic type, letting you choose the type for both the key —the value used for the index—and the value associated with the index. (Note that there are some restrictions regarding the key type—see the sidebar on the next page.) Example 9-1 , which models 299
Image of page 323

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

a traditional printed dictionary, uses strings for the index, and expects a string as the result, so myDictionary in that example would be defined as shown in Example 9-2 . Example 9-2. A dictionary with string keys and string values Dictionary<string, string> myDictionary = new Dictionary<string, string>(); Keys, Comparison, and Hashes To be able to look up entries quickly, dictionaries impose a couple of requirements on keys. First, a dictionary entry’s key must not change in a way that affects comparisons. (This often just means that you should never change a key. However, it’s technically possible to build a type for which certain kinds of changes have no impact on compar- isons performed by the Equals methods. Such changes are invisible to the dictionary.) Second, it should provide a good hash function. To understand the first requirement—that for comparison purposes, keys must not change—consider what changing a key would mean in a printed dictionary. Suppose you look up the entry for bug in your dictionary, and then you cross out the word bug and write feature in its place. The usual way of looking up words will now fail for this entry—the entry was positioned in exactly the right place for when the key was bug . Anyone looking up feature will not think to look in the location for your amended item. And it’s the same with a dictionary collection—to enable fast lookup, dictionaries create an internal structure based on the keys items had when they were added to the dictionary. It has no way of knowing when you’ve changed a key value. If you really need to do this, you should remove the entry, and then add it back with the new key— this gives the dictionary a chance to rebuild its internal lookup data structures. This requirement is most easily met by using an immutable type, such as string , or any of the built-in numeric types. The second requirement—that key types should have a good hash function—is a bit less obvious, and has to do with how dictionary collections implement fast lookup. The base System.Object class defines a virtual method called GetHashCode , whose job is to return an int whose value loosely represents the value of the object. GetHashCode is required to be consistent with the Equals method (also defined by System.Object )— two objects or values that are equal according to Equals are required to return the same hash code. Those are the rules, and dictionaries will not work if you break them.
Image of page 324
Image of page 325
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern