欢迎光临散文网 会员登陆 & 注册

C# ?方法,再也不用string.IsNullOrEmpty

2023-05-15 11:29 作者:大衣哥编程  | 我要投稿

传统做法

判断字符串是否为空,写法繁琐:
 if(string.IsNullOrEmpty(s1))
            {
                // .....
            }


现代方法

string result = s2?.ToString() ?? "";

或 string result = s2?.ToString() ;

解释:

s2?.ToString()  如果s2为null,则直接返回s2

s2?.ToString() ?? ""; 如果s2为null,则返回""


C# ?方法,再也不用string.IsNullOrEmpty的评论 (共 条)

分享到微博请遵守国家法律