| Adam Vandenberg ( @ 2005-10-16 21:21:00 |
| Entry tags: | dotnet |
C# Generic Collections
This article explains that while in C#, string[] is a subclass of object[], List<string> is not a subclass of List<object>.
This makes sense, and in fact, thinking about it (and messing around in VS2005 Express) enlightened me as to some problems in our current non-generic strongly typed collection classes.
(All of our strongly typed lists have methods to add "derived type" and "base type", because of inheritance. But that means the compiler won't complain if you add any derived type to a strongly-typed list, since it can be silenly converted through the base type. Now, if our goal is "compile time type checking" we're sunk; if it's just the convenience of not having to cast in and out of a base type list, then we're OK.)
As an aside, the names of the generic collection interfaces are the same (minus the <T>) as the names of the non-generic collection interfaces, and yet some of them are defined very differently. And I don't even know why Collection<T> is stuck under "System.Collections.ObjectModel" instead of "System.Collections.Generic".