C#'s equivalent of Java's <? extends Base> in generics
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.
Related videos on Youtube
Comments
-
Louis Rhys almost 2 years
In Java, I can do the following: (assume
Subclass
extendsBase
):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 over 13 yearsThere isn't really an equivalent in .net, thank god
-
meandmycode over 13 yearsYour example shows broken type variance in java, .net doesn't have broken variance.. well, apart from one place.
-
Saurabh over 13 yearspossible duplicate of C# inheritance in generics question
-
-
Michael Krauklis over 12 yearsBeautiful, almost exactly what I was looking for. Thanks! public class ImplementingClass<T> : BaseClass<T> where T : GenericBaseClass { ... }
-
Abhijeet Apsunde about 11 yearsThis response actually qualifies as an answer
-
crush over 10 yearsWhat an ugly way to have to accomplish this, but at least it does the job! +1
-
Balázs Édes about 9 yearsJust 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 aBase
implementor, which is not an instance ofSubClass
? Will that be a runtime error? -
maliks almost 8 years@Raystrom--can you answer this question which is related to your answer?
-
maliks almost 8 years@Guilheme--can you answer this question which is related to your answer?
-
maliks almost 8 years@decyclone--can you answer this question which is related to your answer?
-
maliks almost 8 years@Mehrdad--can you answer this question which is related to your answer?
-
maliks almost 8 yearsI 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 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 almost 8 yearsYeah 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 almost 8 yearsIs 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 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 over 6 yearsHere is another variation where your class must implement IDisposable in addition to someBaseClass. public class MyArray<T> : IDisposable where T: someBaseClass
-
Aaron Franke about 6 yearsHow do I use this to restrict T to numbers, such as
float
orint
? -
Raystorm about 6 yearsYou have to use the Class version of the numeric type.