- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我如何传递一个列表,该列表是一个 DerivedObjects 列表,其中方法需要一个 BaseObjects 列表。我正在转换列表 .ToList<BaseClass>()
并且想知道是否有更好的方法。我的第二个问题是语法不正确。我正在尝试传递列表 byref,但出现错误:'ref' argument is not classified as a variable
如何解决这两个问题?谢谢。
public class BaseClass { }
public class DerivedClass : BaseClass { }
class Program
{
static void Main(string[] args)
{
List<DerivedClass> myDerivedList = new List<DerivedClass>();
PassList(ref myDerivedList.ToList<BaseClass>());
// SYNTAX ERROR ABOVE IS - 'ref' argument is not classified as a variable
Console.WriteLine(myDerivedList.Count);
}
public static void PassList(ref List<BaseClass> myList)
{
myList.Add(new DerivedClass());
Console.WriteLine(myList.Count);
}
}
已解决:
类似的方法解决了我的问题。
public static void PassList<T>(ref List<T> myList) where T : BaseClass
{
if (myList == null) myList = new List<T>();
// sorry, i know i left this out of the above example.
var x = Activator.CreateInstance(typeof(T), new object[] {}) as T;
myList.Add(x);
Console.WriteLine(myList.Count);
}
感谢所有帮助解决这个问题和其他 SO 问题的人。
最佳答案
ref
部分很简单:要通过引用传递参数,基本上它必须是一个变量。所以你可以这样写:
List<BaseClass> tmp = myDerivedList.ToList<BaseClass>();
PassList(ref tmp);
...但这不会影响 myDerivedList
的值或内容本身。
ref
无论如何,这是毫无意义的,因为您永远不会更改 myList
的值无论如何在方法内。了解更改参数值与更改参数值所指对象的内容之间的区别很重要。参见 my article on parameter passing了解更多详情。
现在至于为什么不能传入列表 - 这是为了保持类型安全。假设您可以这样做,我们可以这样写:
List<OtherDerivedClass> list = new List<OtherDerivedClass>();
PassList(list);
现在将尝试添加 DerivedClass
的实例到 List<OtherDerivedClass>
- 这就像在一堆香蕉上加一个苹果……没用! C# 编译器会阻止您执行该不安全的操作——它不会让您将一串香蕉当作一个水果盘。假设我们确实有Fruit
而不是 BaseClass
, 和 Banana
/Apple
作为两个派生类,带有 PassList
添加 Apple
到它给出的列表:
// This is fine - you can add an apple to a fruit bowl with no problems
List<Fruit> fruitBowl = new List<Fruit>();
PassList(fruitBowl);
// This wouldn't compile because the compiler doesn't "know" that in PassList
// you're only actually adding an apple.
List<Apple> bagOfApples = new List<Apple>();
PassList(bagOfApples);
// This is the dangerous situation, where you'd be trying to really violate
// type safety, inserting a non-Banana into a bunch of bananas. But the compiler
// can't tell the difference between this and the previous one, based only on
// the fact that you're trying to convert a List<Banana or Apple> to List<Fruit>
List<Banana> bunchOfBananas = new List<Banana>();
PassList(bunchOfBananas );
C# 4 在某些情况下允许generic variance - 但在这种特殊情况下它无济于事,因为您正在做一些根本不安全的事情。虽然通用方差是一个相当复杂的主题 - 由于您仍在学习参数传递的工作原理,我强烈建议您暂时不要管它,直到您对其余部分更有信心的语言。
关于c# - 如何在参数类型为 List<BaseClass> 时传递 List<DerivedClass>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7643211/
在我的游戏中,我有一个 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
我是一名优秀的程序员,十分优秀!