Q: C# cast Foo to Foo does anyone know if it is possible to cast a generic type with a certain type parameter (e.g. Bar) to the same generic type with the type parameter being a base type of Bar (such as object in my case). And, if it is possible, how would it be done?
What I want to do is have a collection of Foo<object> but be able to add Foos with more specific type arguments.
Thanks 

A: You can have a collection of a base type with subclasses added.  For example, the following will work:
// Using:
public class Foo {} // Base class
public class Bar : Foo {} // Subclass

// Code:
List<Foo> list = new List<Foo>();
HashSet<Foo> hash = new HashSet<Foo>();

list.Add(new Bar());
list.Add(new Foo());

hash.Add(new Bar());

Since "Bar" is a specific type of "Foo", it's perfectly legal to add it to a collection of Foo.
However, until .NET 4 and the out modifier for covariance, you cannot do:
IEnumerable<Foo> list = new List<Bar>(); // This isn't supported in .NET 3.5...


A: Use the ConvertAll method of List(T) or Array.
http://msdn.microsoft.com/en-us/library/73fe8cwf.aspx

A: Yes it is possible in C# 4.0!
You should look into covariance.

A: Related: 
How is Generic Covariance & Contra-variance Implemented in C# 4.0?
