我正在尝试在这里打印出一个数组/集合。我有一个带有以下代码的类文件来打印出文本:
//Display All
public void Display()
{
Console.WriteLine(ID + "\t" + Product + "\t" + Category + "\t" + Price + "\t" + Stock + "\t" + InBasket);
}
然后,我主要尝试使用以下方法将其实际打印到屏幕上:
foreach (KeyValuePair<int, Farm_Shop> temp in products)
{
//display each product to console by using Display method in Farm Shop class
temp.Display();
}
但是我收到以下错误:
'System.Collections.Generic.KeyValuePair<int,Farm_Shop_Assignment.Farm_Shop>'
does not contain a definition for 'Display' and no extension method 'Display'
accepting a first argument of type
'System.Collections.Generic.KeyValuePair<int,Farm_Shop_Assignment.Farm_Shop>'
could be found (are you missing a using directive or an assembly reference?)
这是我要打印的实际内容:
products = new Dictionary<int, Farm_Shop>
{
{ 1, new Farm_Shop(1, "Apple", "Fruit\t", 0.49, 40, 'n') },
{ 2, new Farm_Shop(2, "Orange", "Fruit\t", 0.59, 35, 'n') }
};
据我了解,这不起作用,因为我只是发送要打印的数组/集合,而不是要打印的 int,如果您知道我的意思的话。
有人可以告诉我如何让它正确打印。
非常感激。谢谢。