Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

design question: class that inherits from dictionary

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sfdev2000@yahoo.com - 05 Mar 2008 01:56 GMT
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?
Peter Duniho - 05 Mar 2008 02:15 GMT
> [...]
> 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
sfdev2000@yahoo.com - 05 Mar 2008 14:49 GMT
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.
sfdev2000@yahoo.com - 05 Mar 2008 14:53 GMT
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.
Peter Duniho - 05 Mar 2008 18:53 GMT
> I found a performance comparison of C# Dictionary versus
> SortedDictionary
[quoted text clipped - 3 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.