87

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>();
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

4 Answers4

141

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 function
public 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.

Raystorm
  • 6,180
  • 4
  • 35
  • 62
8

Look into Covariance and Contravariance introduced with .Net 4.0. But it only works with interfaces right now.

Example:

IEnumerable<Base> list = new List<SubClass>();
Community
  • 1
  • 1
decyclone
  • 30,394
  • 6
  • 63
  • 80
  • Just looking at this thread, trying to understand the C# way of generics. What happens, if the static type of your example is `List`, and if I want to add a `Base` implementor, which is not an instance of `SubClass`? Will that be a runtime error? – Balázs Édes Mar 23 '15 at 15:30
  • @decyclone--can you answer [this question](http://stackoverflow.com/questions/37806107/getting-behavior-of-javas-class-extends-map-in-net) which is related to your answer? – maliks Jun 14 '16 at 09:09
7

If you are looking for two type generics, Take a look at this:

    void putAll<K1, V1>(Dictionary<K1,V1> map) where K1 : K where V1 : V;
  • @Guilheme--can you answer [this question](http://stackoverflow.com/questions/37806107/getting-behavior-of-javas-class-extends-map-in-net) which is related to your answer? – maliks Jun 14 '16 at 09:09
7

There is no exact equivalent (since the type system doesn't work in quite the same way, with type erasure and all), but you can get very similar functionality with in and out using covariance and contravariance.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • @Mehrdad--can you answer [this question](http://stackoverflow.com/questions/37806107/getting-behavior-of-javas-class-extends-map-in-net) which is related to your answer? – maliks Jun 14 '16 at 09:09
  • I have this problem since number of days, and this is second time I asked such question, can't get that why people can't answer it to the point – maliks Jun 14 '16 at 09:15
  • @Taufel: At first glance at least it looks like an "XY problem". What are you *actually* trying to do? Are you just trying to construct something? Because there's probably a way to do it that doesn't involve exactly matching the Java semantics (which may not even be possible). – user541686 Jun 14 '16 at 09:18
  • Yeah I really want to do it with or without exactly matching the Java semantics but the end result (functionality) should be same at most – maliks Jun 14 '16 at 09:20
  • Is this really a difficult question to answer and by the way, less people used to approach this question? Hope you would do help in this regard :) – maliks Jun 14 '16 at 09:36
  • @Mehrdad--would you please like to ping me that are you going to help in this regard (so that I'll wait) or I have to go for some other? – maliks Jun 14 '16 at 09:52