collection classes

M

Mike P

I'm pretty new to OOP and I'm just starting looking at generics and
collection classes. I understand why I might want to create a business
class, but can somebody please explain to me the reasons behind creating
collection classes?


Many thanks,

Mike
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mike said:
I'm pretty new to OOP and I'm just starting looking at generics and
collection classes. I understand why I might want to create a business
class, but can somebody please explain to me the reasons behind creating
collection classes?

With .NET 2.0 and generics you will probably not need to create
collection classes - just use them.

You need them whenever you have more than one.

Arne
 
S

sloan

If you just need a list of business objects, then


List < myobject > newList = new List < myobject > ();

would suffice.

however, sometimes you want to have the collection have its own
functionality.


therefore

MyObjectCollection : List <myobject>

is appropriate.

and you can tack on methods there.


Here is an interesting implementation:

http://www.mattberther.com/?p=592
 
B

Brian Gideon

I'm pretty new to OOP and I'm just starting looking at generics and
collection classes. I understand why I might want to create a business
class, but can somebody please explain to me the reasons behind creating
collection classes?

Many thanks,

Mike

*** Sent via Developersdexhttp://www.developersdex.com***

You may want to create a custom collection class if there are
operations that make sense to exist on a collection of a specific
type. Since you can't add properties or methods to an existing class
you'll have to subclass or create a brand new class. For example, if
you have an Account business entity then it might make sense to have
an AccountCollection class where you could define a TotalBalance
property that would return the net balance of all contained accounts.

In the pre 2.0 days it was common to create a custom collection to
strongly type its contained items. But, with generics that's no
longer necessary.
 
J

Jon Skeet [C# MVP]

What uses are there for generics? Are they only used with collection
classes?

Collections are the principal use, but not the only use. They're
useful wherever you want to enforce type-safety at the caller of the
class, but the class itself doesn't need to know what type is involved
(or is able to express the constraints in a generic way).

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top