List.AddRange有一些无效的参数

时间:2011-04-11 13:14:20

标签: list generics interface

我想创建一个符合预定义IFlower界面的花卉列表(例如),如下所示。

public interface IFlower
{
    string colour { get; }
    int petals { get; }
}

public class Rose : IFlower
{
    public Rose()
    {
        string[] colours = new string[]{ "Pink", "Orange", "Red", "Crimson", "Cerise" };
        Random random = new Random();
        colour = colours[random.Next(0, 4)];
    }

    public string colour { get; set; }

    public int petals
    {
        get { return 8; }
    }
}

public class Daisy : IFlower{
    public Daisy()
    {
        string[] colours = new string[]{ "White", "Yellow", "Purple" };
        Random random = new Random();
        colour = colours[random.Next(0, 2)];
    }

    public string colour { get; set; }

    public int  petals
    {
        get { return 18; }
    }
}

public class Flowers
{
    public List<Daisy> Daisies
    {
        get
        {
            List<Daisy> items = new List<Daisy>();
            items.Add(new Daisy());
            items.Add(new Daisy());
            return items;
        }
    }

    public List<Rose> Roses
    {
        get
        {
            List<Rose> items = new List<Rose>();
            items.Add(new Rose());
            items.Add(new Rose());
            return items;
        }
    }
}

问题是,当我运行以下代码来创建连接列表时:

    public List<IFlower> Flowers
    {
        get
        {
            List<IFlower> output = new List<IFlower>();
            output.AddRange(Daisies);
            output.AddRange(Roses);
            return output;
        }
    }

我收到错误:

The best overloaded method match for 'System.Collections.Generic.List<IFlower>.AddRange(System.Collections.Generic.IEnumerable<IFlower>)' has some invalid arguments

1 个答案:

答案 0 :(得分:1)

玫瑰和雏菊令牌必须各自引用实现

的类的实例
IEnumerable<IFlower>

所以我猜他们中的一个或两个都引用了别的东西。