471,318 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Generic Dictionary For Keys Only?

All,

I have a situation where I need a List<stringthat performs like a
generic dictionary (Dictionary<string, string>). Essentially, I just
need the key, not the value, so I want to use as little memory as
possible. Currently I am setting all the values of the dictionary to
null.

The List<stringdoesn't work because I am doing a lot of .Contains()
or .ContainsKey(), and lookup speed is pretty critical.

So, does such a thing exist? A dictionary that only uses a key, with
no value? Or does making the value null eliminate any memory overhead
anyway?
Thanks for any insight.

Phil
Jun 27 '08 #1
3 1933
On May 27, 3:17 pm, Phil Sandler <psandle...@hotmail.comwrote:

<snip>
So, does such a thing exist? A dictionary that only uses a key, with
no value? Or does making the value null eliminate any memory overhead
anyway?
What you're after is a set, basically. There *is* one in .NET 3.5:
System.Collections.Generic.HashSet. If you're not using .NET 3.5, I
don't believe there's one available in the framework - it's easy to
build the right interface using Dictionary, although that won't get
you any efficiency improvements.

How many elements are you likely to have in here? In many cases any
resource usage improvement will be negligible.

Jon
Jun 27 '08 #2
[...]

On May 27, 11:44 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
What you're after is a set, basically. There *is* one in .NET 3.5:
System.Collections.Generic.HashSet. If you're not using .NET 3.5, I
don't believe there's one available in the framework - it's easy to
build the right interface using Dictionary, although that won't get
you any efficiency improvements.

How many elements are you likely to have in here? In many cases any
resource usage improvement will be negligible.
We are not currently using 3.5--not sure if we need VS2008 to get
there? If so, we probably can't do so on this project; if not, then
it might be worth it if these Hashsets would make a big difference.

There will be a number of these collections in the system, of varying
sizes. The biggest ones may have at most 100K items in the list, with
(probably) a max of 20 lists of this size. The rest of the lists will
be small, probably no more than a few hundred items.

Thanks for your response.

Phil
Jun 27 '08 #3
Phil Sandler <ps********@hotmail.comwrote:
How many elements are you likely to have in here? In many cases any
resource usage improvement will be negligible.

We are not currently using 3.5--not sure if we need VS2008 to get
there? If so, we probably can't do so on this project; if not, then
it might be worth it if these Hashsets would make a big difference.
Yes, you need VS2008 to use .NET 3.5. Of course, there are other big
reasons for using VS2008 - all the goodness of C# 3, for one thing.

I do understand the need to not change technologies mid-project,
however.
There will be a number of these collections in the system, of varying
sizes. The biggest ones may have at most 100K items in the list, with
(probably) a max of 20 lists of this size. The rest of the lists will
be small, probably no more than a few hundred items.
Well, do a test - but I doubt it will cost you vast amounts of
resources.

You may well find there are free 3rd party implementations around.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by none | last post: by
4 posts views Thread by Adam Clauss | last post: by
18 posts views Thread by Rune B | last post: by
7 posts views Thread by Sehboo | last post: by
5 posts views Thread by Ethan Strauss | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.