In Java, I can do the following: (assume Subclass extends Base):
ArrayList<? extends Base> aList = new ArrayList<Subclass>();
What is the equivalent in C# .NET? There is no ? extends keyword apparently and this does not work:
List<Base> aList = new List<Subclass>();
Actually there is an Equivalent(sort of), the
wherekeyword. I don’t know how “close” it is. I had a function I needed to do something similar for.I found an msdn page about it.
I don’t know if you can do this inline for a variable, but for a class you can do:
public class MyArray<T> where T: someBaseClassor for a function
public T getArrayList<T>(ArrayList<T> arr) where T: someBaseClassI didn’t see it on the page but using the
wherekeyword it might be possible for a variable.