473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Collection Classes, Generics and C# 2

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/Ge...ustomClass.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
Jun 4 '07 #1
7 5724
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
Jun 4 '07 #2
thanks marc,

so for the most part, using the System.Collections.ObjectModel.Collection
will be good enough.

Jun 4 '07 #3
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

Jun 4 '07 #4
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<Tor List<Tand you'll be fine. When you need
something more complex, you'll know ;-p

Marc
Jun 4 '07 #5
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<TAstar()

{

}

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 .

"CodeRazor" <Co*******@discussions.microsoft.comwrote in message
news:D1**********************************@microsof t.com...
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/Ge...ustomClass.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


Jun 4 '07 #6

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<Tor 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.

On Mon, 4 Jun 2007 01:16:03 -0700, CodeRazor
<Co*******@discussions.microsoft.comwrote:
>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/Ge...ustomClass.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
Jun 4 '07 #7
thanks for your help.
it was useful.
Jun 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Stewart | last post by:
Let's say I create a collection of DateTime objects like so: Dim clxn_Times As Collection = New Collection() Let's go on to say I add 3 elements to the collection. How do I pull off the...
2
by: Kiran A K | last post by:
hi, suppose in my application i need to perform a lot of insertions and deletions on my data structure. the ideal data structure in this scenario would be a linked list. unlike java, c# does not...
1
by: Radu | last post by:
Hi, I have VS.NET 2002 and I'm reading about ReadOnlyCollectionBase, Collection, and DictionaryBase. In the following reference regarding VB2005, I can't find them: ...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
4
by: Robert Schneider | last post by:
Hi, the msdn documentation provides an Dinosaurs example that uses the objectmodel.collection class. Just have a look on the help page for this class. I don't understand what happens here. Or...
14
by: cwineman | last post by:
Hello, I'm hoping to do something using Generics, but I'm not sure it's possible. Let's say I want to have a bunch of business objects and a data access class cooresponding to each business...
1
by: rh1200la | last post by:
Hey all. Here's my question. I'm building a Shipping rates component with the following classes: Package which contains basic package information. I also have shipper classes that implement...
8
by: Mike P | last post by:
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...
0
by: manwood | last post by:
I have a generic method that performs some basic sorting and removal of duplicates on some classes, but I cannot get the parameter to pass into the Distinct function working. This is my class...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.