Generics and your family tree
One thing that can be counterintuitive about generics is the way it kind of “breaks” inheritance.
For example, suppose you have the following two lists:
List < string > stringList = new List < string >();
List < object > objectList;
You might expect to be able to cast stringList to objectList like so:
objectList = (List < object > )stringList;
Because, after all, a string inherits from object, so this shouldn’t be a problem, but it actually is.
When you make a List < string > instance, you’re inheriting from List < T >. Similarly, when you declare objectList as List < object > , you’re again inheriting from List < T >. What results from this is that the types of these two lists (List < string > and List < object >) are actually siblings in the inheritance hierarchy, both having a parent of List.
While inheritance is powerful, it’s not incestuous
Don’t try to mix siblings