FormatDate definition

本文关键字:definition FormatDate | 更新日期: 2023-09-27 18:04:15

我有DateTime属性DateCreation,它不是空的。我可以这样做:

String test=myObject.DateCreation.FormatDate();

如果对象上的DateTime属性是空的,像这样:

public virtual DateTime? LastInteractionDate { get; set; }

我不能这样做:

String test=myObject.LastInteractionDate.FormatDate();

错误是:DateTime吗?不包含'FormatDate'的定义

如何解决有空字符串如果null?

FormatDate definition

您可以使用.Value来获取底层值类型:

String test = myObject.LastInteractionDate.Value.FormatDate();

和检查null:

String test = myObject.LastInteractionDate != null
    ? myObject.LastInteractionDate.Value.FormatDate()
    : string.Empty;

在这种情况下,如果LastInteractionDate属性为null,结果将是一个空字符串