C#相当于Java的?在泛型中扩展Base>

时间:2021-05-18 17:31:09

In Java, I can do the following: (assume Subclass extends Base):

在Java中,我可以执行以下操作:(假设Subclass扩展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:

C#.NET中的等价物是什么?没有?显然扩展关键字,这不起作用:

List<Base> aList = new List<Subclass>();

4 个解决方案

#1


79  

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.

实际上有一个Equivalent(有点),where关键字。我不知道它是多么“接近”。我有一个功能,我需要做类似的事情。

I found an msdn page about it.

我找到了一个关于它的msdn页面。

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

我不知道你是否可以内联一个变量,但对于一个类你可以这样做:public class MyArray 其中T:someBaseClass或函数public T getArrayList (ArrayList arr)其中T:someBaseClass

I didn't see it on the page but using the where keyword it might be possible for a variable.

我没有在页面上看到它,但使用where关键字可能是变量。

#2


8  

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

研究.Net 4.0引入的协方差和逆变。但它现在只适用于接口。

Example:

IEnumerable<Base> list = new List<SubClass>();

#3


5  

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.

没有确切的等价物(因为类型系统不能以完全相同的方式工作,具有类型擦除和所有),但是您可以使用协方差和逆变来获得非常类似的功能。

#4


1  

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;

#1


79  

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.

实际上有一个Equivalent(有点),where关键字。我不知道它是多么“接近”。我有一个功能,我需要做类似的事情。

I found an msdn page about it.

我找到了一个关于它的msdn页面。

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

我不知道你是否可以内联一个变量,但对于一个类你可以这样做:public class MyArray 其中T:someBaseClass或函数public T getArrayList (ArrayList arr)其中T:someBaseClass

I didn't see it on the page but using the where keyword it might be possible for a variable.

我没有在页面上看到它,但使用where关键字可能是变量。

#2


8  

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

研究.Net 4.0引入的协方差和逆变。但它现在只适用于接口。

Example:

IEnumerable<Base> list = new List<SubClass>();

#3


5  

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.

没有确切的等价物(因为类型系统不能以完全相同的方式工作,具有类型擦除和所有),但是您可以使用协方差和逆变来获得非常类似的功能。

#4


1  

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;