C#的相当于Java的&LT ;?扩展底座>在仿制药 [英] C#'s equivalent of Java's <? extends Base> in generics

查看:218
本文介绍了C#的相当于Java的&LT ;?扩展底座>在仿制药的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我能做到以下几点:(假设子类扩展基本):

 的ArrayList和LT ;?扩展底座> ALIST =新的ArrayList<子类>();
 

什么是相当于C#.NET?没有?扩展关键字显然,这不工作:

 名单,其中,基地> ALIST =新的名单,其中,子类>();
 

解决方案

其实,有(差不多)等价物,在其中,关键字。我不知道它是怎么亲密是。我有我需要做类似的东西的功能。

我发现了一个<一个href="http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx#csharp_generics_topic4">msdn关于它的页面。

我不知道,如果你能做到这一点内嵌一个变量,但对于一个类,你可以这样做:
公共类MYARRAY&LT; T&GT;其中T:someBaseClass
或为一个函数
公共牛逼getArrayList&LT; T&GT;(ArrayList的&LT; T&GT; 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.

这篇关于C#的相当于Java的&LT ;?扩展底座&GT;在仿制药的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆