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