在Java中,我能做到以下几点:(假设子类
扩展基本
):
的ArrayList和LT ;?扩展底座> ALIST =新的ArrayList<子类>();
什么是相当于C#.NET?没有?扩展
关键字显然,这不工作:
名单,其中,基地> ALIST =新的名单,其中,子类>();
![各行业最受欢迎的编程语言](https://p.xsw88.cn/allimgs/daicuo/20230902/832.png)
解决方案
其实,有(差不多)等价物,在其中,
关键字。我不知道它是怎么亲密是。我有我需要做类似的东西的功能。
我发现了一个msdn关于它的页面。
我不知道,如果你能做到这一点内嵌一个变量,但对于一个类,你可以这样做:
公共类MYARRAY< T>其中T:someBaseClass
或为一个函数
公共牛逼getArrayList< T>(ArrayList的< T> ARR)其中T:someBaseClass
我没有看到它在页面上,但使用where`关键字有可能为一个变量。
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>();
解决方案
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.
相关推荐
最新文章