tags:

views:

373

answers:

7

When I programin C#, there are times I need a strongly typed collection:

I often create a class that inherits from the ArrayList:

using System.Collections;
public class Emails: ArrayList
{
  public new Email this[int i]
  {
     get
     {
        return (Email)base[i];
     }
     set
     {
        base[i] = value;
     }
  }
}

I realize this is probably not the correct way to inherit from a collection. If I want to inherit from a strongly typed collection in C#, how should I do it, and what class should I choose to inherit from?

+2  A: 

Not really, that's what generics are for.

Otávio Décio
+3  A: 

In general new should be avoided for member declaration if at all possible.

As of C# 2 though you can use generics. List<email> list = new List<email>();

dkackman
+5  A: 

If you're not stuck with .Net 1.1, you really should forget about ArrayList and use the generic List<T> instead. You get strong typing and better performances to boot.

Yann Schwartz
+13  A: 

What you want is a generic, like List<T>.

public class Emails : List<Email>
{
}

This has all of the methods of ArrayList and then some, and you get type safety without having to do any extra work.

Note, however, that inheriting from List<T> can sometimes cause you more trouble than it's worth. A better idea is to implement ICollection<T> or IEnumerable<T>, and use the List<T> internally to implement the interface.

JSBangs
Thanks to every one here for answering so quickly. I'll change my bad habit right away!
Tom
Love this method. ;) I use it a LOT when I'm coding and need a group of objects with quick indexers.
Zack
Don't inherit from List<T>. Just use it. If you really want a new type, create an alias.
Joel Coehoorn
-1 Not the right answer. Deriving from List<T> is frought with problems. +1 to Joel Coehoorn.
George Stocker
@Joel - wouldn't the alias have to be repeated in every file?
Kobi
Do NOT use alias. :) It's terrible in C#.
Vasiliy Borovyak
+1  A: 

Generics seem much more appropriate. For example,

List<email>

in this case. Why reinvent the wheel and perform worse?

Clay Fowler
A: 

If you use .net 2.0 or + i would go with a generic collection

Yassir
+8  A: 

Forget both ArrayList and List. The "right" thing to do would be to derive from Collection and call your class something that ends in Collection.

public sealed class EmailCollection : Collection<Email>
{

}
Josh Einstein
@Josh- I got your point. I agree about the syntax you use (emailCollection) make more sense and you know exactly at what you referring to! But what make you choose among all the different possibilities the dotnet framework is offering: the collection.[using System.Collections.ObjectModel;]This has mostly the same methods than the List or the ArrayListIs that something performance wise, or just a matter of taste and habit ?
Tom
Well, Collection(T) is designed to be used as a base class. So if you were going to derive your own class to expose in an object model, that would be the one I'd use. If are not exposing it, then I'd use List but wouldn't derive from it - I'd just create a List(Email). As for naming it, it's just a convention FooCollection for example as opposed to Foos.
Josh Einstein
Using Collection(T) or IEnumerable(T) is a good habit to get into for modeling your domain.Other thing to think about: When you expose this collection to callers will they be modifying it? Will it be common for them to want to access elements by index? Will they need to know the number of elements in the collection?Very often a Collection(T) will suffice, and then you haven't exposed functionality that is not meaningful. If your collection goes over the wire, for example, e.g. WCF, does it make sense for it to have an Add() method? Shouldn't Add() happen over your service layer instead?
Leon Breedt