日期:2014-05-17  浏览次数:20799 次

C# 得出判断是否为空的最好方法

1.最普遍、最直接的方法
string Input = "Yours Input Msg";
if (Input == "");
???????????
2.使用String.Empty
string Input = "Yours Input Msg";
if (Input == String.Empty);

?

3.使用string.Length
string Input = "Yours Input Msg";
if (Input.Length == 0);

?

借鉴一些文章得下好的判断方法,省空间,省内存,效率高

这样,比起Input == ""这个方法,效率大约提高13-30多倍(因为有短路运算),空间却只要1/7。

因此这些方法中判断字符串是否为空的最佳方法是:

if(desStr!=null && desStr.Length==0)

?