C#'s equivalent of Java's <? extends Base> in generics

56,764

Solution 1

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.

Solution 2

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>();

Solution 3

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;

Solution 4

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.

Share:
56,764

Related videos on Youtube

Louis Rhys
Author by

Louis Rhys

trying to learn and help others learn :)

Updated on July 05, 2022

Comments

  • Louis Rhys
    Louis Rhys almost 2 years

    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>();
    
    • meandmycode
      meandmycode over 13 years
      There isn't really an equivalent in .net, thank god
    • meandmycode
      meandmycode over 13 years
      Your example shows broken type variance in java, .net doesn't have broken variance.. well, apart from one place.
    • Saurabh
      Saurabh over 13 years
      possible duplicate of C# inheritance in generics question
  • Michael Krauklis
    Michael Krauklis over 12 years
    Beautiful, almost exactly what I was looking for. Thanks! public class ImplementingClass<T> : BaseClass<T> where T : GenericBaseClass { ... }
  • Abhijeet Apsunde
    Abhijeet Apsunde about 11 years
    This response actually qualifies as an answer
  • crush
    crush over 10 years
    What an ugly way to have to accomplish this, but at least it does the job! +1
  • Balázs Édes
    Balázs Édes about 9 years
    Just looking at this thread, trying to understand the C# way of generics. What happens, if the static type of your example is List<Base>, and if I want to add a Base implementor, which is not an instance of SubClass? Will that be a runtime error?
  • maliks
    maliks almost 8 years
    @Raystrom--can you answer this question which is related to your answer?
  • maliks
    maliks almost 8 years
    @Guilheme--can you answer this question which is related to your answer?
  • maliks
    maliks almost 8 years
    @decyclone--can you answer this question which is related to your answer?
  • maliks
    maliks almost 8 years
    @Mehrdad--can you answer this question which is related to your answer?
  • maliks
    maliks almost 8 years
    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
  • user541686
    user541686 almost 8 years
    @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).
  • maliks
    maliks almost 8 years
    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
    maliks almost 8 years
    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
    maliks almost 8 years
    @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?
  • JeredM
    JeredM over 6 years
    Here is another variation where your class must implement IDisposable in addition to someBaseClass. public class MyArray<T> : IDisposable where T: someBaseClass
  • Aaron Franke
    Aaron Franke about 6 years
    How do I use this to restrict T to numbers, such as float or int?
  • Raystorm
    Raystorm about 6 years
    You have to use the Class version of the numeric type.