Nullable ToString()

前端 未结 6 1923
清歌不尽
清歌不尽 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:28

    Maybe it is just to follow a pattern? Or they don't know the backend. You are right that the code is exactly the same. You can even do:

    int? i = null;
    i.ToString(); //No NullReferenceException
    

提交回复
热议问题