design question: class that inherits from dictionary

S

sfdev2000

I created a generic collection class that is derived from dictionary.

I create an instance of this class to hold objects of a specific
class.

What I want to do is make it optional for an instance of my generic
class to be sorted, and so I'm trying to figure out if there is any
way to make my generic collection class work with dictionary or
sorteddictionary based on what constructor I call for my generic
collection class (or any other way to achieve the same result).

class foo : dictionary
{
}

I want to be able to create an instance of foo that is either based on
"sorteddictionary" or "dictionary" depending upon what type of object
(class) I want to store in it.

Is this at all possible?
 
P

Peter Duniho

[...]
I want to be able to create an instance of foo that is either based on
"sorteddictionary" or "dictionary" depending upon what type of object
(class) I want to store in it.

Is this at all possible?

No. You can't have conditional inheritance.

However, it's a great example of why composition is often better than
inheritance anyway. It'd be nice if C# had a more convenient way of
exposing composited interfaces than manually writing stubs to wrap an
internally stored class implementing the interface. But other than that
inconvenience (something shared by other popular OOP languages anyway),
it's not bad.

So, just make your collection class expose whatever interface(s) you need,
and then use the appropriate collection class internally to store the data.

I assume that your desire to optionally use a Dictionary<> instead of
SortedDictionary<> is a performance issue? Because otherwise, I'd think
you'd just use a SortedDictionary<> all the time.

Pete
 
S

sfdev2000

Pete,

Thanks. For most of the collections that I'm using my class for they
do not need to be sorted and I am a big fan of trying to focus on
performance in everything I code. If SortedDictionary's performance
is not that different than Dictionary's performance than I'll leave
things as-is :)

I was hoping I might be missing something clever that I could do with
interfaces and inheritance to accomplish what I wanted to do.

I'll take a look at composition.
 
P

Peter Duniho

I found a performance comparison of C# Dictionary versus
SortedDictionary

http://www.artima.com/forums/flat.jsp?forum=152&thread=179998

and so I'll now take a look at using composition instead of
inheritance.

Right. For small collections, it's unlikely you'd notice the theoretical
disparity in performance. But sorting data definitely adds a significant
overhead from a code analysis point of view, and for large enough data
this overhead will be noticeable and significant.

If you've got large collections, and performance is an issue, you're not
going to want to sort data that doesn't need to be sorted.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top