Apr 282013
 
 April 28, 2013  Posted by at 12:52 pm C# & F#, Not So Stupid Questions  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

I was refactoring my Leap Motion demo code earlier today and had one of my colleagues from Telerik help me out, Jeffrey Fritz  . A second pair of eyes is extremely valuable for code reviews (Stupid Question 103: What are some different types of code reviews?), not just for code quality – but also to learn something new.

The library I was using had the Gesture types as enums, and somewhere in my code was a big old ugly switch. I made a comment about a ‘that’s what you get for using enums’ (jokingly) and Jeff said, ‘Well, in that particular case you could use a lookup dictionary’). I was confused. Lookup or dictionary? You see, in case you didn’t know there is a class called Lookup, and then there is the Dictionary class. What is even more confusing is that the term lookup dictionary also exists, it might or might not refer to an actual class 😀 Now, for my particular case it made more sense to use a Dictionary, but I’ve used Lookup quite a few times.

Lookup or Dictionary? (And what is Lookup)

Lookup or Dictionary? (And what is Lookup)

  • Dictionary and Lookup can be used as lookup structures, but there are some differences between the two.
  • Lookup has no public constructor and is immutable. Immutable means that we cannot add or remove items after creation. (‘Stupid’ Question 30: What do we mean by immutable and mutable in OO?)
  • Lookup can have duplicate keys
  • Lookup is created by the factory method ToLookup() found on classes that implement IEnumerable
  • Searching for a non-existing key will return an empty sequence instead of an exception
  • Another big difference is that Lookup maps to a collection of values, not a single value like Dictionary does.

I ended up choosing Dictionary and making it readonly as the values were just strings, the keys where unique, and for me creating a collection with enums and then use a method to create a Lookup seemed like the wrong way to go. Performance wise I’ve been told the two don’t differ, but I haven’t tested that myself.

I’m curious to see if I’ve missed something above in regards to differences, which use scenarios you can think of, and why one should choose one over another?

  6 Responses to “Stupid Question 181 & 182: Lookup or Dictionary? (And what is Lookup)”

  1. No, you pretty much got the gist of it. An off-the-cuff example demonstrating the difference between the two would be linking authors to books they have written (TKey = Author and TElement = Book). If you were to use a Dictionary, then you’d only be able to link the author to her latest book, while with a Lookup, you could link the author to a collection of books she has written.

    • @Ray
      On the other hand, If you make the book the key of the dic you could link several books to a author. A book is unique. The only thing about this approach is that each book only can have one author, which is not the case.

      Even though I know dictionaries are faster, I never use them. I don’t like the code and as an excuse I always say that in the future you might and up getting problems with the unique key :p

    • I like that comparison 🙂 I might steal it 😉

  2. Just MSDN-Docs provide the most efficient definition what a Lookup is: “Represents a collection of keys each mapped to one or more values.”

    • I agree that is a compact and good definition. I would still argue that when it comes to making the right choice, Dictionary or Lookup, knowing the details and the differences is valuable I reckon. Imagine if one expected them to behave the same way – one would be in for a few surprises 😉

  3. One of the things I like to see in data structure comparisons is a discussion of their performance characteristics – this can be a useful way to think about why one might be preferable over another in certain situations, particularly when they otherwise support a similar set of operations.

    Here, it seems that the Dictionary documentation has some remarks about performance characteristics, but Lookup does not. I understand why the “interface” documentation authors might want to steer clear of implementation details, but I’m curious as to why a Lookup is immutable, and whether it has any bearing on performance.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 14 + 9 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)