加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#中NULL,"",DBNULL,String.Empty,Convert.IsDBNull

(2009-01-08 14:27:49)
标签:

it

分类: ASP.NET

(1)NULL

          null 关键字是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,如果 int i=null,的话,是不可以的,因为Int是值类型的。

(2)DBNULL

      DBNull在DotNet是单独的一个类型,该类只能存在唯一的实例,DBNULL.Value,DBNull唯一作用是 可以表示数据库中的字符串,数字,或日期,为什么可以表示原因是DotNet储存这些数据的类(DataRow等)都是以 object 的形式来储存数据的。对于 DataRow , 它的 row[column] 返回的值永远不为 null , 要么就是具体的为column 的类型的值 。 要么就是 DBNull 。 所以 row[column].ToString() 这个写法永远不会在ToString那里发生NullReferenceException。DBNull 实现了 IConvertible 。 但是,除了 ToString 是正常的外,其他的ToXXX都会抛出不能转换的错误。

(3)""和String.Empty

这两个都是表示空字符串,其中有一个重点是string str1="" 和 string str2=null 的区别,这样定义后,str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的,string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将抱错。

(4)Convert.IsDBNull()

     Convert.IsDBNull()返回有关指定对象是否为 DBNull 类型的指示,即是用来判断对象是否为DBNULL的。其返回值是True或Flase。

 

string.Empty不分配存储空间
      ""分配一个长度为空的存储空间   
      所以一般用string.Empty

为了以后跨平台,还是用string.empty

在 C# 中,大多数情况下 "" 和 string.Empty 可以互换使用。比如:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifstring s = "";
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
string s2 = string.Empty;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif
if (s == string.Empty) http://www.cnblogs.com/Images/dot.gif{
C#中NULL,"",DBNULL,String.Empty,Convert.IsDBNull  
// http://www.cnblogs.com/Images/dot.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif
}

if语句成立


判定为空字符串的几种写法,按照性能从高到低的顺序是:

s.Length == 0      优于 s == string.Empty      优于 s == ""


您关于String.Empty和Null的问题是这样的,这两个都是表示空字符串,其中有一个重点是string str1= String.Empty和 string str2=null 的区别,这样定义后,str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的,string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将报错。textBox1.Text的值为零长度字符串 ""。

String.Empty vs. ""

So, I just converted a bunch of code that used "" as an empty string like this:

if(myString == "") anotherString = "";

to

if(myString.Equals(String.Empty))   anotherString = String.Empty;

and I'm wondering if I did the right thing, using String.Empty. I hate having quoted strings in code and prefer to stay away from them whenever possible.   This generally leads to code that is more strongly typed, and easier to maintain, so using String.Empty intuitively feels better than ““.   But, I've actually found a concrete reason to use String.Empty - I did some research and found that in a test, str.Equals(String.Empty) is faster than comparing to ""   Well, okay. Research isn't the right word, “Doing one google search and accepting on faith the first post I saw” is slightly more accurate.   I even created a little graph of the claims in this post here, that String.Empty is faster.   I'm too lazy to do the test myself, so if you want to verify this, knock yourself out.   I do love making graphs though.

http://www.codebetter.com/bsblog/stringtime.gif
也就是说 判断字符串是否为空best的方法就是     str.Length==0 !

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有