【问题标题】:Displaying var = ... firstOrDefault as a string and not typename [duplicate]将 var = ... firstOrDefault 显示为字符串而不是 typename [重复]
【发布时间】:2018-02-21 18:26:46
【问题描述】:

如何格式化此 var 以显示返回的日期?

var lastDropStop = shipmentStatus.Stops.LastOrDefault(x => x.StopType.ToUpper() == "DROP");
debugFile.WriteLine("lastDropStop=" + lastDropStop.ToString());

我看到的不是日期,而是类型名称:

lastDropStop=MyComp.Outbound.Business.Entities.Models.ShipmentStatusStop

【问题讨论】:

  • 您只需要访问 ShipmentStatusStop 类型的日期属性吗? debugFile.WriteLine("lastDropStop=" + lastDropStop.WhateverIsTheDateProperty.ToString()); 没有对象定义很难知道。
  • ToString() 的默认行为是打印出类型名称。虽然您可以覆盖它,但通常这是不必要的。只需按照其他海报所说的那样使用您想要打印的属性。

标签: c# formatting tostring


【解决方案1】:

你正在打印对象,你也应该这样写属性名

debugFile.WriteLine("lastDropStop=" + lastDropStop.StopType.ToString()); // or lastDropStop.LastDropStop.ToString() or whatever the property name is

【讨论】:

  • 是的,我完全以为我是在约会,而不是整个对象。
猜你喜欢
  • 2023-01-18
  • 2018-06-22
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多