Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Collection Classes, Generics and C# 2

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CodeRazor - 04 Jun 2007 09:16 GMT
Can someone explain a few things about collections to me.

In C# 2 generics, you can create a collection class by inheriting from
System.Collections.ObjectModel.Collection. Using this you can iterate through
the collection and use "foreach" on the collection.

public class Cars : System.Collections.ObjectModel.Collection<Car> { }

So, could someone explain to me why you would want to create a GENERIC
custom collection class.

See http://www.codeproject.com/dotnet/GenericCustomClass.asp  -- "Writing
Custom Collection Classes" section

I haven't really used collection classes or had to write one for .Net 1.1,
so i am a little perplexed.

Many thanks, CR
Marc Gravell - 04 Jun 2007 09:30 GMT
Well, as a thought, you might want to abstract the implementation from
the public API - especially if your code is being consumed by external
parties. That way, you can change the internals without breaking the
clients - for instance, switching to a dictionary of some kind.

This is also necessary if the base container is sealed (or doesn't
provide many virtuals), and you want to customize the behaviour - i.e.
extra methods (can't do if sealed) or extra checks (can't do if not
virtual). You can also limit the methods available - perhaps Clear()
isn't sensible for your scenario?

Marc
CodeRazor - 04 Jun 2007 10:01 GMT
thanks marc,

so for the most part, using the System.Collections.ObjectModel.Collection
will be good enough.
Jon Skeet [C# MVP] - 04 Jun 2007 10:06 GMT
On Jun 4, 10:01 am, CodeRazor <CodeRa...@discussions.microsoft.com>
wrote:
> so for the most part, using the System.Collections.ObjectModel.Collection
> will be good enough.

Well, unless you need to add extra functionality, I'd normally use the
collections in System.Collections.Generic, personally.

Jon
Marc Gravell - 04 Jun 2007 10:29 GMT
I've looked at the article... the author has a generic collection
inheriting from CollectionBase - so isn't exactly good gode to follow
;-p Plus a good half-dozen other glaring mistakes... (new ToString(),
GENERICTYPE vs GenericType [won't build], Item[int] instead of
this[int], etc - plus common style/naming conventions). IMHO, not a
good article.

Stick to Collection<T> or List<T> and you'll be fine. When you need
something more complex, you'll know ;-p

Marc
Fred Mellender - 04 Jun 2007 12:23 GMT
Note that you do not need to inherit from Collection in order to implement
the "foreach" construct.  Just write a method that returns IEnumerable<T>,
as in

public IEnumerable<T> Astar()

{

}

which I implemented in a class called Graph.

This allows you to use the "yield return" construct (in Astar) so that
callers can invoke the "foreach" function on the method, as in

foreach (KSnode ksn in graph.Astar())

{

}

This makes Astar look like a collection to your users, without your needing
to inherit from collection (or even making use of List<T>, etc).

For other examples of using generics in combination with the "foreach"
construct, in order to implement collection-like behavior,  see
http://www.frontiernet.net/~fredm/dps/Contents.htm .

> Can someone explain a few things about collections to me.
>
[quoted text clipped - 15 lines]
>
> Many thanks, CR
Samuel R. Neff - 04 Jun 2007 13:58 GMT
The advantage of generics is that you generally don't need custom
collection classes any more.  In .NET 1.1 people used to create custom
collections so that they could enforce a constraint on the type of the
contents of the collection.  With Generics in .NET 2.0 you no longer
need a custom class for this.

Most of the time you'll just use List<T> or Dictionary<TKey, TValue>
directly instead of creating a custom collection.

The exception is when you need different behavior.  That is a far
rarer situation than simply different contents.  One example of
different behavior from my current application is a custom KeySet<T>
collection.  This is basically a dictionary without a value, just keys
as I've found myself often needing to use a dictionary to store a
unique set of keys and checking existence in the collection without
caring about any actual value.  Implementing this as a custom generic
class made usage of the class cleaner than when using
IDictionary<T,T>.

HTH,

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.

>Can someone explain a few things about collections to me.
>
[quoted text clipped - 14 lines]
>
>Many thanks, CR
CodeRazor - 06 Jun 2007 11:43 GMT
thanks for your help.
it was useful.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.