我有一个程序,可以在引号列表中添加不同的引号。 我在我的代码中将Quote添加到我的列表“ Quotes”中,然后尝试打印“ Quotes”并打印 “ System.Collections.Generic.List'1 [GetQuotes.Quote]” 如何获取要打印的值?
public static void LoadData()
{
Quotes = new List<Quote>();
Quotes.Add(new Quote(){
HaulierName = "Hellmans",
FulfillmentCenter = "BHX4",
PalletQty = 2,
Price = 122
});
Quotes.Add(new Quote(){
HaulierName = "Pallet Online",
FulfillmentCenter = "BHX4",
PalletQty = 2,
Price = 111.98
});;
Console.WriteLine(Quotes);
Console.ReadLine();
}
答案 0 :(得分:2)
您需要为ToString()
类重写Quote
方法。
请参阅:https://docs.microsoft.com/en-us/dotnet/api/system.object.tostring?view=netframework-4.8
您可以通过以下方式做到这一点:
class Program {
public class Quote {
public string HaulierName { get; set; }
public string FulfillmentCenter { get; set; }
public int PalletQty { get; set; }
public double Price { get; set; }
public override string ToString() {
return $"{HaulierName} {FulfillmentCenter} {PalletQty} {Price}";
}
}
static void Main(string[] args) {
{
var Quotes = new List<Quote>();
Quotes.Add(new Quote() {
HaulierName = "Hellmans",
FulfillmentCenter = "BHX4",
PalletQty = 2,
Price = 122
});
Quotes.Add(new Quote() {
HaulierName = "Pallet Online",
FulfillmentCenter = "BHX4",
PalletQty = 2,
Price = 111.98
});
foreach (var item in Quotes) {
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
如果您真的想拥有:
Console.WriteLine(Quotes);
您可以在内部实现自己的集合类和ovverride ToString()
方法。