如何在c#的泛型列表中存储不同类型的对象

本文关键字:存储 同类型 对象 泛型 列表 | 更新日期: 2023-09-27 18:11:17

我需要添加不同类型的对象(尽管它们不同,但它们共享相同的基类,它们是兄弟姐妹)。

考虑这个类层次

public abstract class Fruit
{
    public int Quality { get; set; }
    public string Name { get; set; }
}
public sealed class Apple :Fruit
{
    public string Color { get; set; }
    public Apple(string color ="Red")
    {
        Color = color;
    }
}
public sealed class Orange : Fruit
{
    public Orange(string type = "WithSeed")
    {
        Type = type;
    }
    public string Type { get; set; }
}

我该怎么做呢

_apples = new List<Apple>
        {
            new Apple
            {
                Name = "Fiji Apple",
                Quality = 9,
                Color ="Green"
            },
            new **Orange**
            {
                Name = "Indi Orange",
                Quality = 7,
            }
        };

这个泛型列表的使用者将通过基类使用它。例如:使用IEnumerable<Fruit>。有人能建议一下吗?

如何在c#的泛型列表中存储不同类型的对象

有一个Fruit列表。这样,列表将保存任何Fruit对象以及继承自Fruit类型的任何对象。

List<Fruit> _list = new List<Fruit>
{
    new Apple
    {
        Name = "Fiji Apple",
        Quality = 9,
        Color ="Green"
    },
    new Orange
    {
        Name = "Indi Orange",
        Quality = 7
    }
};

使用类层次是一个常见的错误。有一个接口,并为该接口创建一个List,然后你甚至可以存储不属于该类层次结构的对象。

当你需要存储一个不同类型的对象时,只需实现该类型的接口,这样你就不必对存储列表代码做任何更改。

因为所有继承自Fruit的对象都足以创建FruitList

 List<Fruit>_apples = new List<Fruit>{...}

如果我有一个执行not extend Fruit的类,则必须创建List of objects,因为所有类型(预定义的值类型和引用)都直接或间接地继承自Object

List<Object>_apples = new List<Object>{...}

请记住,如果你不扩展水果不能访问他们的属性,如qualitycolor

列表必须始终知道它所包含的类型。因此,为了使它同时包含AppleOrange,您必须将列表定义为包含涵盖您打算放入列表中的所有项的任何超类或接口。

在这种情况下,这将使List<Fruit>List<object>可能同时包含这两个。

使用下面的代码在List中添加不同的对象

List<Fruit> fruits = new List<Fruit>();
            fruits.Add(new Apple("Red"));
            fruits.Add(new Orange("Withseed"));

我相信你要做的是使用不同类型的Fruit列表只是为了存储它们或将它们传递到一个List中,而不是需要单独的List<Apple>, List<Orange>等。但是您仍然需要使用特定类型的东西(如Apple.ColorOrange.Type)。

为了演示这一点,我创建了一个FruitBasket类来存储所有不同的Fruit对象和Project()对象及其独特的属性。

public class FruitBasket
{
    public List<Fruit> FruitList { get; }
    public FruitBasket(List<Fruit> fruitList)
    {
        FruitList = fruitList;
    }
    public List<string> Project()
    {
        var result = new List<string>();
        foreach (var fruit in FruitList)
        {
            if (fruit is Apple)
            {
                var apple = (Apple) fruit;
                result.Add("This is a " + apple.Color + " " + apple.Name);
            }
            else if (fruit is Orange)
            {
                var orange = (Orange) fruit;
                result.Add("A " + orange.Name + " with type: " + orange.Type);
            }
            else
            {
                result.Add("An unknown " + fruit.Name);
            }
        }
        return result;
    }
}

当循环遍历所有不同的Fruit对象时,它检查水果的类型并在列表中放入一个唯一的字符串。

这个例子的用法是:

var result = new FruitBasket(new List<Fruit>
{
    new Apple
    {
        Name = "Fiji Apple",
        Quality = 9,
        Color = "Green"
    },
    new Orange
    {
        Name = "Indi Orange",
        Quality = 7,
    }
}).Project();

result将是:

这是一个绿色的斐济苹果

字体:WithSeed