Nullable ToString()

前端 未结 6 1938
清歌不尽
清歌不尽 2020-12-13 22:39

I see everywhere constructions like:

int? myVar = null;
string test = myVar.HasValue ? myVar.Value.ToString() : string.Empty;

Why not use

6条回答
  •  忘掉有多难
    2020-12-13 23:16

    I know, long after it was relevant, but ... I suspect it is because for nullable types like int? the .ToString() method does not allow you to use format strings. See How can I format a nullable DateTime with ToString()? . Perhaps in the original code, there was a format string in .ToString(), or perhaps the coder forgot that .ToString() without the format string was still available on nullable types.

提交回复
热议问题