- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试传递 DerivedClass
的列表到接受 BaseClass
列表的函数,但我收到错误:
cannot convert from
'System.Collections.Generic.List<ConsoleApplication1.DerivedClass>'
to
'System.Collections.Generic.List<ConsoleApplication1.BaseClass>'
现在我可以投 List<DerivedClass>
了到 List<BaseClass>
,但除非我明白为什么编译器不允许这样做,否则我觉得这样做不舒服。
我发现的解释只是说它以某种方式违反了类型安全,但我没有看到。谁能帮帮我?
编译器允许从 List<DerivedClass>
转换的风险是什么?至 List<BaseClass>
?
这是我的 SSCCE:
class Program
{
public static void Main()
{
BaseClass bc = new DerivedClass(); // works fine
List<BaseClass> bcl = new List<DerivedClass>(); // this line has an error
doSomething(new List<DerivedClass>()); // this line has an error
}
public void doSomething(List<BaseClass> bc)
{
// do something with bc
}
}
class BaseClass
{
}
class DerivedClass : BaseClass
{
}
最佳答案
是因为List<T>
是不变,而不是协变,所以你应该改为IEnumerable<T>
支持协变:
IEnumerable<BaseClass> bcl = new List<DerivedClass>();
public void doSomething(IEnumerable<BaseClass> bc)
{
// do something with bc
}
关于covariant in generic的信息.
关于c# - 无法从 List<DerivedClass> 转换为 List<BaseClass>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966961/
在我的游戏中,我有一个 Loot 基类,它具有对任何可以被玩家捡起并存储在他的元素栏中的东西通用的方法。这将包括药水、设备、弹药等。我可以装备箭,但不能装备药水。所以 Arrow 将是 Ammo 的一
有 class BaseClass implements IData (); class ChildClassA() extends BaseClass; class ChildClassB() ex
例如,在 vanilla JS 中我可以轻松地执行以下操作: class BaseClass extends React.Component { ... } class Foo extends Bas
我正在自动化一个由多个独立应用程序组成的环境。在某些时候,我决定将每个应用程序定义为一个类并将其保存为单独的文件是最有意义的。 我现在拥有的: 包含 *py 文件的目录,其中每个文件为单个应用程序定义
我有一个基类和许多子类。在每个子类中,我都使用对象 A - 因此我将它放在基类中。在一半的子类中,我使用对象 B,因此我没有将 B 放在基类中。我希望我的基类包含尽可能多的功能,因此如果定义了对象 B
我有: class BASE{ public: virtual void func1() = 0; }; 然后我有一些派生类,比如: class Derived1 : public BASE{
(抱歉标题模糊) 我有一个带有各种派生类 D1、D2 等的基类 B。 另外还有一个模板 template Storage; 现在每个派生类都有不同版本的存储类 class D1 : public c
请考虑以下类别: public interface BaseInterface { void method1(); void method2(); void method3()
我无法在具有 2 个输入引脚的 BaseClasses 中找到合适的渲染器类 - 一个用于视频,一个用于音频。有人可以推荐/提供一些代码吗? 问候多米尼克 最佳答案 CBaseRenderer cla
我正在学习一门新的 C++ 语言,其中使用了 Java 中使用的知识 not JavaScript .我正在尝试创建一个名为 Command 的父类派生类 CmdHelp .在 Java 中我会制作一
基本上我有一个名为 Asset 的类,它包含我系统中某个 Asset 的所有信息。这可能会变得相当大( Assets 有缩略图、文件名、元数据、评级、评论等)。 在我的结果页面上,我列出了所有符合特定
我正在尝试创建一个简单的元素“ list ”,就像在任何角色扮演游戏中一样。我制作了非常基本的类,它们具有属性。 无论如何,我有一个基类 item从那里继承的是weapon . item具有也在 we
我即将为我的 Razor 组件创建一个基类。 这个 Base-Class 看起来像: public abstract class ExampleBase : ComponentBase { p
我正在开发一款可以追溯到很久以前的软件产品。我们源代码的一部分是构建基类的项目。据推测,这是一个随 Windows SDK 一起提供的项目,用于开发引用 DirectShow 的应用程序。 不幸的是,
Internet 上有很多关于在覆盖 Equals 时如何覆盖 GetHashCode() 的信息。但是,所有这些示例都是关于包含一些可以生成哈希的字段的类。我试图找到的是一个很好的 GetHashC
我有很多类都继承自一个基类——实际上它只是一个允许序列化的接口(interface)包装器: public abstract class VoucherRuleBase : IRule{ pu
我的 java 程序中有一系列类,如下所示: src | |- BaseCost |- com.jdoe.nationalcosts |- EntryTicketCost |- Fli
编辑:这是我的错,因为还有另一个 Foo潜伏的定义让我对Foo<>感到困惑定义。 在下面的代码中,Foo是 Bar 的基类对吧? 那为什么编译器会在 .BaseType 下划线呢?部分并说“给定的表达
我有一个名为 GLObject 的基类,具有以下 header : class GLObject{ public: GLObject(float width = 0.0, fl
命名基类的推荐方法是什么?是在类型名称前加上“Base”或“Abstract”还是我们只在其后缀“Base”? 考虑以下几点: 类型:ViewModel 例如MainViewModel、ReportV
我是一名优秀的程序员,十分优秀!