I am looking for a equivalent syntax for java code below in C#.
ArrayList<Class <? extends A>> list = new ArrayList<Class<? extends A>>();
// Sample usage
list.add(A.class);
list.add(B.class); // B extends A
The list above basically only accept Sub Class of A (or A) as input.
Thank in advance.
There's no equivalent for that in C#. The generics in C# are quite different to those in Java in various ways, including the way covariance and contravariance work.
You could have a generic method (or generic type) with a constraint on the type parameter like this:
public void Foo<T>(IList<T> list) where T : SomeClass
or
public class Foo<T> where T : SomeClass
... but Type
itself (the .NET equivalent of Class<T>
) is non-generic, so there's no way of saying "This is a list of types which extend A
".
If you give us more information about what you're trying to achieve, we may be able to help you more.
See more on this question at Stackoverflow