C# Nullable to string

后端 未结 11 1872
挽巷
挽巷 2020-12-16 09:29

I have a DateTime? variable, sometimes the value is null, how can I return an empty string \"\" when the value is null or

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 10:11

    You could write an extension method

    public static string ToStringSafe(this DateTime? t) {
      return t.HasValue ? t.Value.ToString() : String.Empty;
    }
    
    ...
    var str = myVariable.ToStringSafe();
    

提交回复
热议问题