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

4

91

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>();
Unorganized answered 19/1, 2011 at 6:51 Comment(3)
There isn't really an equivalent in .net, thank godRecto
Your example shows broken type variance in java, .net doesn't have broken variance.. well, apart from one place.Recto
possible duplicate of C# inheritance in generics questionAncheta
H
144

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.

Hormone answered 11/10, 2011 at 20:3 Comment(7)
Beautiful, almost exactly what I was looking for. Thanks! public class ImplementingClass<T> : BaseClass<T> where T : GenericBaseClass { ... }Hedva
This response actually qualifies as an answerIntracranial
What an ugly way to have to accomplish this, but at least it does the job! +1Opuscule
@Raystrom--can you answer this question which is related to your answer?Boarding
Here is another variation where your class must implement IDisposable in addition to someBaseClass. public class MyArray<T> : IDisposable where T: someBaseClassEda
How do I use this to restrict T to numbers, such as float or int?Thessalonians
You have to use the Class version of the numeric type.Hormone
B
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>();
Bawl answered 19/1, 2011 at 6:55 Comment(2)
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?Cesarcesare
@decyclone--can you answer this question which is related to your answer?Boarding
T
8

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;
Tisiphone answered 29/7, 2015 at 1:5 Comment(1)
@Guilheme--can you answer this question which is related to your answer?Boarding
L
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.

Lindholm answered 19/1, 2011 at 6:54 Comment(6)
@Mehrdad--can you answer this question which is related to your answer?Boarding
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 pointBoarding
@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).Lindholm
Yeah I really want to do it with or without exactly matching the Java semantics but the end result (functionality) should be same at mostBoarding
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 :)Boarding
@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?Boarding

© 2022 - 2024 — McMap. All rights reserved.