2007-06-07

Are Baboons Animals (in a generic sense)

I was writing a generic class in C# today and generics is great and all that, but it's not quite complete in it's design (not in todays target anyway). I will give you an example.

Say that you declare an Animal class

public class Animal {
public Animal(string division, string className,
string order, string family, string genus){}
}

And then we declare the Baboon...

public class Baboon : Animal {
public Baboon() :
base("Chordata","Mammalia","Primates",
"Cercopithecidae","Papio") {}
}

All is well, a standard object inheritance (even one with an anchor in the real world (oh yeah!), and a scientific twist (bring it on!))

Now we would like a generic collection for the animals that only can take animals.

public class BaseAnimalCollection<T> : List<T>
where T : Animal{}

Cool, now this type of collection can contain all sorts of animals and we could add methods common to all animal life, like... like... well, Clone and Exterminate are the first that comes to mind... (think I should read something more than scientific magazines to broaden my horizon?)

Anyway...
We start using the collection

BaseAnimalCollection<Animal> myAnimalCollection =
new BaseAnimalCollection<Animal>();

Here we can put som animals in...

We can also specify a collection that only can contains Baboons (if we want to use the common animal functionality that is provided with the collection (Clone and Exterminate ;)

BaseAnimalCollection<Baboon> myBaboonCollection =
new BaseAnimalCollection<Baboon>();

The strength with generics is that we can, with little effort (code), create a typed collection that behaves as a collection should and we don't need to fear any runtime nuisances since the type checking is done in compilation time.

So when I would like to create my best of animal collections (everyone has these nowadays) I just create a typed collection and add on...

BaseAnimalCollection<Animal> myBestAnimalCollection =
new BaseAnimalCollection<Animal>();
//add some animals
myBestAnimalCollection.Add(new Animal(
"Arthropoda", "Arachnida", "Acarina.Parasitiformes",
"Ixodoidea", "Amblyomma"));
//add a Baboon !!!
myBestAnimalCollection.Add(new Baboon());
//even add my own list of animals to the list.
myBestAnimalCollection.AddRange(myAnimalCollection);

So far all is good. Now I want to add my baboon list to the best of collection. Since Baboons are animals (and I have already added a Baboon to the list) this should be ok . So we add the code below.

myBestAnimalCollection.AddRange(myBaboonCollection);

Now we get two compilation errors...

The best overloaded method match for 'System.Collections.
Generic.List.AddRange(System.Collections.Generic.
IEnumerable)' has some invalid arguments

Argument '1': cannot convert from 'BaseAnimalCollection'
to 'System.Collections.Generic.IEnumerable'

Why is this? Why can't we convert from a collection of Baboons to a enumerable of animals? Aren't Baboons animals when it comes to Generics or what?

Give me your best shot.

Good luck!
/Dan

No comments:

Post a Comment