C#,如何将派生类的列表传递给接收基类列表的方法?

发布于 2025-01-11 12:24:32 字数 1014 浏览 1 评论 0 原文

这是我的代码的简化版本:

using System.Collections.Generic;

public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(List<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes[0].item;
    }
}

public class Apple
{
}

public class AppleBox : FruitBox<Apple>
{
}

public class FruitShop
{
    List<AppleBox> appleBoxes = new List<AppleBox>();

    public void Main()
    {
        AppleBox appleBox = new AppleBox();
        appleBoxes.Add(appleBox);

        AppleBox.ChooseFirst(appleBoxes); // => Error here
    }
}

我的行中有一个错误:

AppleBox.ChooseFirst(appleBoxes);

无法从 System.Collections.Generic.List 转换为 System.Collections.Generic.List>

我尝试过:

AppleBox.ChooseFirst((List<FruitBox<Apple>>)appleBoxes);

但同样的错误。

我该如何继续?

This is a simplified version of my code:

using System.Collections.Generic;

public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(List<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes[0].item;
    }
}

public class Apple
{
}

public class AppleBox : FruitBox<Apple>
{
}

public class FruitShop
{
    List<AppleBox> appleBoxes = new List<AppleBox>();

    public void Main()
    {
        AppleBox appleBox = new AppleBox();
        appleBoxes.Add(appleBox);

        AppleBox.ChooseFirst(appleBoxes); // => Error here
    }
}

I have an error in the line:

AppleBox.ChooseFirst(appleBoxes);

cannot convert from System.Collections.Generic.List<AppleBox> to System.Collections.Generic.List<FruitBox<Apple>>

I tried:

AppleBox.ChooseFirst((List<FruitBox<Apple>>)appleBoxes);

But same error.

How do I have to proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2025-01-18 12:24:32

此处解释了这种行为的原因。简而言之 - 类不支持 variance< C# 中的 /a> 和 List 不是 List>

你可以做什么:

  • “转换”集合(实际上创建一个新集合):

使用 OfType<>().ToList()

AppleBox.ChooseFirst(appleBoxes.OfType<FruitBox<Apple>>().ToList())

或只是 ToList

AppleBox.ChooseFirst(appleBoxes.ToList<FruitBox<Apple>>())
  • 更改 ChooseFirst 签名与协变一起使用 IEnumerable接口:
public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(IEnumerable<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes.First().item;
    }
}

The reason for such behaviour is explained here. In short - classes do not support variance in C# and List<AppleBox> is not List<FruitBox<Apple>>.

What you can do:

  • "convert" collection (actually create a new one):

with OfType<>().ToList()

AppleBox.ChooseFirst(appleBoxes.OfType<FruitBox<Apple>>().ToList())

or just ToList

AppleBox.ChooseFirst(appleBoxes.ToList<FruitBox<Apple>>())
public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(IEnumerable<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes.First().item;
    }
}
寻找一个思念的角度 2025-01-18 12:24:32

您必须将派生类的引用保存到基类变量中

List<FruitBox<Apple>> appleBoxes = new List<AppleBox>();

FruitBox<Apple> appleBox = new AppleBox();
appleBoxes.Add(appleBox);

appleBox.ChooseFirst(appleBoxes);

You will have to hold the reference of the derived class into the base class variable

List<FruitBox<Apple>> appleBoxes = new List<AppleBox>();

FruitBox<Apple> appleBox = new AppleBox();
appleBoxes.Add(appleBox);

appleBox.ChooseFirst(appleBoxes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文