Solved

Implementing collections inheriting from either CollectionBase or ArrayList

Posted on 2003-10-24
6
307 Views
Last Modified: 2011-04-14
Are there advantages to creating custom collections in .NET inheriting from the ArrayList class versus the CollectionBase class?  As far as I can tell, using either method is about equal.  Are there performance advantages to using one over the other?  Ease of use seems to be the same.  How about available functionality between the two choices?

Thanks!

Tom
0
Comment
Question by:Tomscarb
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:Tomscarb
ID: 9616045
OK, After a quick read on MSDN I guess I can partially answer my own question - ArrayList implements the ICloneable interface where CollectionBase does not, allowing me to quickly copy the values in my collection to a new instance in a single command.  That's cool - is there any other consideration to choosing one versus the other?
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 125 total points
ID: 9616753


Personally I would choose to inherit from the ArrayList class.

CollectionBase class is an abstract class with not a lot of options.  

Other classes that inherit from the ColletionBase abs class include many classes from the system.diagnostics namespace.

CounterCreationDataCollection, EventLogPermissionEntryCollection and PerformanceCounterPermissionEntryCollection to name a few


Also, something to note (it may not be important), but the CollectionBase class uses the arraylist class as a return value for the property InnerList.

So if you were looking for performance differences, there will not be any (unless the CollectionBase class adds some overhead, which I doubt since it is abstract).

go with the arraylist class, it has more options and the IClonable interface makes it much more powerful
0
 
LVL 6

Assisted Solution

by:zrh
zrh earned 125 total points
ID: 9617003
Well you can inherit from the CollectionBase, and IClonable in your new class.

public class MyCollection : CollectionBase, ICloneable {
    ...
}

Your supposed to inherit from CollectionBase because it allows you to have a strongly typed collection.
And thats what its there for. I would inherit from CollectionBase.

Hope that helps,
ZRH
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9617165

ZRH-

>> CollectionBase because it allows you to have a strongly typed collection

you mentioned that CollectionBase enforces strongly typed collections, doesn't the ArrayList class enforce strong typing as well?

What benifit does an object gain by being strongly typed vs weak typing?

0
 
LVL 6

Expert Comment

by:zrh
ID: 9617247
First here's a quote from msdn in the CollectionBase overview:
"Notes to Implementers:  This base class is provided to make it easier for implementers to create a strongly typed custom collection. Implementers should extend this base class instead of creating their own."

Being strongly typed means that it returns the type without having to cast it to it.

For example
Weak Typing:
-----------------------
ArrayList a = new ArrayList();
a.Add(MyTypeObject);
MyType mto = (MyType)a[0];
mto.DoSomething();

Strong Typing:
----------------------------
MyCollection mc = new MyCollection();
mc.Add(MyTypeObject);
mc[0].DoSomething();

ArrayList returns type Object, and if you override it you still have to return object as the type.

Hope that helps,
ZRH

0
 

Author Comment

by:Tomscarb
ID: 10628070
Many thanks to both of you for your comments, and many apologies for not closing the question and rewarding points sooner.

-Tom
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question