Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Implementing collections inheriting from either CollectionBase or ArrayList

Posted on 2003-10-24
6
Medium Priority
?
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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