List.AddRange Has Some Invalid Arguments
I want to create a list of flowers (for example) that conform to a predefined IFlower
interface as below.
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;
}
}
}
The problem is, when I run the following code to create the concatenated list:
public List<IFlower> Flowers
{
get
{
List<IFlower> output = new List<IFlower>();
output.AddRange(Daisies);
output.AddRange(Roses);
return output;
}
}
I get the error:
The best overloaded method match for 'System.Collections.Generic.List<IFlower>.AddRange(System.Collections.Generic.IEnumerable<IFlower>)' has some invalid arguments
The Roses and Daisies tokens must each refer to an instance of a class implementing
IEnumerable<IFlower>
So I guess that one or both of them refers to something else.
精彩评论