Oosterman
Oosterman

Reputation: 384

What is the correct way of using a Dictionary in C#?

I have (lots of) objects Foo with an unique ID and want to store these in a Dictionary. The dictionary key in C# can be any primitive type or object. I could use the integer foo1.ID as key but also the object foo1.

Which is the correct way of implementing that and is there a difference in performance using either the ID (an integer) or the object as key?

NB. The values in the dictionary are other (type of) of objects.

Upvotes: 1

Views: 553

Answers (5)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239764

How do you intend to search the dictionary? If you intend to search for items within the dictionary based purely on ID, then use that as the key. OTOH, if you're going to have an instance of a Foo, then make that the key.


Re: your edit - now we know that the Foo is either "the key" or "the object that provides the key value by accessing a property", then it seems simple to say, use a Dictionary<Foo,OtherClass> - assuming you've set up equality comparisons on Foo objects appropriately - why force every instance of lookup to know to extract a specific property from the Foo objects?

Upvotes: 2

Shyju
Shyju

Reputation: 218852

Dictionaries are Key Value Pairs. Each Key should be Unique. The Compiler has to make sure the Keys are unique. By Giving the Key as an object instead of an integer, You are probably doing an overkill. The compiler has compare to check the whole object in the Key to make sure it is unique. So i would go for Integer Key if that help you to identify your record uniquely.

Upvotes: 1

Andrew
Andrew

Reputation: 14457

Whatever you use as a key has to be able to be compared. For primitive types, equality is defined generally as you would expect. For objects, you would be testing reference equality unless you define another way to compare the objects, which you can do by passing the appropriate type of IComparer in the Dictionary constructor.

In your case, however, keying to the int is likely to be simplest. You would gain no real benefit from using the object as its own key. You can create the dictionary simply by taking your collection of Foo objects and doing something like:

IDictionary<int, Foo> fooDictionary = fooCollection.ToDictionary(f => f.ID);

Searching the dictionary will be more efficient than simply searching the collection for the given ID each time in most cases.

Upvotes: 1

Ry-
Ry-

Reputation: 225125

Use the ID - if you already have the object, there's no point in looking it up, too.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160982

It depends on your use case. Assuming you want to look up objects given their key value you of course want the id to be the key. That you are asking this question makes me think maybe you don't want a dictionary at all - if you just need to keep a collection of items use a List<T> instead - dictionaries are for mapping a key (e.g. an id) to a value (e.g. a custom object).

Upvotes: 2

Related Questions