Everything inherits from object. It’s the basis of inheritance. Everything can be implicitly cast up the inheritance tree, ie.
object me = new Person();
Therefore, following this through to its logical conclusion, a group of People would also be a group of objects:
List<Person> people = new List<Person>(); people.Add(me); people.Add(you); List<object> things = people; // Ooops.
Except, that won’t work, the people who designed .NET either overlooked this, or there’s a reason, and I’m not sure which. At least once I have run into a situation where this would have been useful, but I had to end up using a nasty hack (subclassing List just to implement a cast operator).
The question is this: is there a reason for this behaviour? Is there a simpler solution to get the desired behaviour?
For the record, I believe the situation that I wanted this sort of behaviour was a generic printing function that displayed lists of objects by calling ToString() and formatting the strings nicely.
OK, everyone who has used generics in .net must have run into this at one point or another.
Yes, intuitively it should work. No, in the current version of the C# compiler it doesn’t.
Eric Lippert has a really good explanation of this issue (it’s in eleven parts or something and will bend you mind in places, but it’s well worth the read). See here.
edit:
dug out another relevant link, this one discusses how java handles this. See here