Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Implementing collections inheriting from either CollectionBase or ArrayList

Posted on 2003-10-24
6
Medium Priority
?
334 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 500 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 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

772 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