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>();
" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Actually there is an Equivalent(sort of), the where
keyword. 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: someBaseClass
or for a functionpublic T getArrayList<T>(ArrayList<T> arr) where T: someBaseClass
I didn't see it on the page but using the where
keyword it might be possible for a variable.
Look into Covariance
and Contravariance
introduced with .Net 4.0
. But it only works with interfaces
right now.
IEnumerable<Base> list = new List<SubClass>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With