Solved

Implementing collections inheriting from either CollectionBase or ArrayList

Posted on 2003-10-24
6
301 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Performance  Html.BeginForm vs jQuery 3 50
C# replace string in void with dR["myData"].ToString() 3 41
Runtime Error 2 28
Finally almost got it 5 23
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

13 Experts available now in Live!

Get 1:1 Help Now