c# – 如果属性为null,则在字符串中写“?”
发布时间:2020-12-15 19:40:04 所属栏目:百科 来源:网络整理
导读:我怎么能像“?”一样写Startted是Startdate是null public DateTime? StartDate { get; set; }public override string ToString(){ return String.Format("Course {0} ({1} is an {2} course,will be given by {3},starts on {4},costs {5:0.00} and will ha
我怎么能像“?”一样写Startted是Startdate是null
public DateTime? StartDate { get; set; } public override string ToString() { return String.Format("Course {0} ({1} is an {2} course,will be given by {3},starts on {4},costs {5:0.00} and will have maximum {6} participants",Name,CourseId,CourseType,Teacher,(StartDate == null ? "?" : StartDate),Price,MaximumParticipants); } 解决方法
您可以调整现有代码
(StartDate == null ? "?" : StartDate.ToString()) 或者利用Nullable< T> .HasValue (StartDate.HasValue ? StartDate.ToString() : "?") 关键是,?:两种条件都需要相同的类型. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |