Solved

Implementing collections inheriting from either CollectionBase or ArrayList

Posted on 2003-10-24
6
297 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
Comment Utility
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
Comment Utility


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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 22

Expert Comment

by:_TAD_
Comment Utility

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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now