如题
解决方案 »
- 判断文本框内容是否改变
- 求教关于C#串口通信的问题
- MSChart 自动填充数据问题
- c#图形问题
- 在C#中拖动一个TreeView上的结点时 , 如何把该结点的Tag中保存的东西传给目标控件.?
- 设置ComboBox控件的Enabled为false时,其字体的颜色
- ImageFormat.save(Ms,ImageFormat.jepg) 耗时太长的问题、
- 请高手帮忙!!!
- 请问关于读取远程xml文件的问题
- 我在用visual c# 做动态报表,请熟悉这方面的朋友指点。(高分放送100)
- 如何在C#DataGridView中打印出网格线
- C# 双击程序功能正常,可是Process或者开机启动功能不能用,纳闷中?
2.if(a==String.Empty)
3.if(a.Length==0)http://www.csharp360.com/bbs/viewthread.php?tid=131&extra=page%3D1
if(string.IsNullOrEmpty(str))
{}
string.IsNullOrEmpty();
另外: public static readonly String Empty = ""; MS 源码;
http://www.cnblogs.com/homezzm/archive/2009/11/27/1611741.html
如果不为null 应该是3快
if(a=="")
if(a==String.Empty)
if(a.Length==0)
整数判断等于最快,没有经过实例化等复杂的过程
……
MyHelper.ToString()代码如下:
public static string ToString(object pObj)
{
try
{
return Convert.ToString(pObj).Trim();
}
catch
{
return "";
}
finally
{ }
}
用reflector 查看System.String 类型的定义:
知:(1)System.String.IsNullOrEmpty 定义如下:
public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}
(2)至于System.String 的实例的Length属性,不用管它,Length==0字符串肯定为空;由(1)、(2)知someString.Length==0;和System.String.IsNullOrEmpty(someString)是一样样的;(3)(不知道什么原因,我的reflector无法查看System.String中的重载运算符“==”),Ms给的“==”重载的说明是:public static bool operator ==(string a, string b)
Member of System.StringSummary:
Determines whether two specified strings have the same value.Parameters:
a: The first string to compare, or null.
b: The second string to compare, or null.Returns:
true if the value of a is the same as the value of b; otherwise, false.如果用someString==""来用,我觉得可能会经过更多的步骤来判断someString 和空字符串的关系,效率可能会比System.String.IsNullOrEmpty稍低。
(4)System.String.Empty的定义是:
public static readonly String Empty = "";
所以someString==System.Empty 和someString==""应该是一样的。
应该使用string.IsNullOrEmpty()方法.
此方法既判断该字符串是否为Null值又判断是否为""值.
一举两得!
1.if(a=="")
2.if(a==String.Empty)
3.if(a.Length==0)
用不等于~~效率搞点!呵呵
我觉得还是这条高!
if(a==""||a==null)