I am new to C#
, How to use below generic for C#, i want the same as below Java
statement in C#
,
List<? extends MySuperClass> list= new ArrayList<MySubClass>();
Java
allows above, can we achieve same in C#
?
I am new to C#
, How to use below generic for C#, i want the same as below Java
statement in C#
,
List<? extends MySuperClass> list= new ArrayList<MySubClass>();
Java
allows above, can we achieve same in C#
?
C# does not have wildcards. So the simple answer is no, there is no direct equivalent.
However, I don't think there is much point in writing that line of code in Java, because you are throwing away type information. You may as well write
List<MySubClass> list = new ArrayList<MySubClass>();
The main reason for using wildcards in Java is to allow method arguments to be as general as possible, like this:
static void foo(List<? extends MySuperClass> list)
You can do that with C# like this:
static void foo<T>(IList<T> list) where T : MySuperClass