:
public DateTime DOB { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? NullableDOB
{
get => DOB.ToNullable();
set => DOB = value.DefaultIfNull();
}
ToNullible(), DefaultIfNull() IsNull() - :
public static T? ToNullable<T>(this T source) where T : struct
=> source.IsNull(default(T)) ? (T?)null : source;
public static T? ToNullable<T>(this T source, T @default) where T : struct
=> source.IsNull(@default) ? (T?)null : source;
public static T DefaultIfNull<T>(this T? source, T @default) where T : struct
=> source ?? @default;
public static T DefaultIfNull<T>(this T? source) where T : struct
=> DefaultIfNull(source, default(T));
public static bool IsNull<T>(this T source) where T : struct
=> source.Equals(default(T));