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

(2)""和String.Empty
   这两个都是表示空字符串。只不过""理论上重新开辟内存空间,而String.Empty指向一处。不过优化器会优化的!

string.Empty不分配存储空间, ""分配一个长度为空的存储空间,所以一般用string.Empty,为了以后跨平台,还是用string.empty。

在C# 中,大多数情况下 "" 和 string.Empty 可以互换使用。比如:

string s = "";

string s2 = string.Empty;

if (s == string.Empty) {//} if语句成立

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

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

注意:

1.string str1="" 和 string str2=null 的区别。

str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的,

string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将报错。

 static void Main(string[] args)
{
string a = "";
string b = null;
string c = string.Empty;
string d = " "; Console.WriteLine(a.Length.ToString());
Console.WriteLine(c.Length.ToString());
try
{
Console.WriteLine(b.Length.ToString());
}
catch (Exception)
{
Console.WriteLine("baoccuole");
}
if (string.IsNullOrEmpty(a))
{
Console.WriteLine("a");
}
if (string.IsNullOrEmpty(b))
{
Console.WriteLine("b");
}
if (string.IsNullOrEmpty(c))
{
Console.WriteLine("c");
}
if (string.IsNullOrEmpty(d))
{
Console.WriteLine("d");
}
if (a == c)
{
Console.WriteLine("a == c");
}
if (a == b)
{
Console.WriteLine("a == b");
}
if (c == b)
{
Console.WriteLine("c == b");
} Console.ReadLine();
}

string.Empty与null与""的更多相关文章

  1. string.empty和null的区别

    关于String.Empty和Null的问题是这样的,这两个都是表示空字符串,其中有一个重点是string str1= String.Empty和 string str2=null 的区别,这样定义后 ...

  2. string.Empty, "" 和 null 三者的区别

    转载:http://www.cnblogs.com/mxxblog/archive/2013/08/22/3275387.html 这是一个及其常见的问题,网上已经有关于这个问题的很多讨论.但是我觉得 ...

  3. asp.net(c#)中String.Empty、NULL、"" 三者到底有啥区别和联系?

    开门见山,首先看下面代码,你认为结果分别是什么? string str = string.Empty; string str1 = ""; string str2 = null; ...

  4. C#中String.Empty、NULL与""三者的区别

    String.Empty和""是一样的,都是空,习惯用string.empty. Null和他们就有区别了,就是没有值,也没分配地址,此处可以理解成什么都没有.

  5. String.Empty、null、“” 区别

    概念准备: 1.引用类型是将对象是实际数据保存在堆中, 将对象在堆中的地址保存在栈中. 2.值类型直接将实际数据存放在堆中,不会将对象在堆中的地址保存在栈中. 一.String.Empty和" ...

  6. string.empty , "" , null 以及性能的比较

    一:这种结论,个人觉得仍然存疑 http://www.cnblogs.com/wangshuai901/archive/2012/05/06/2485657.html 1.null    null 关 ...

  7. string、Empty和null三者的区别

    string.Empty和null三者的区别 本文转自  http://www.bitscn.com/pdb/dotnet/201003/181883.html 时间:2010-03-01 00:00 ...

  8. String.Empty,NULL和""的区别

    String.Empty,NULL和""的区别 string.Empty就相当于"" 一般用于字符串的初始化 比如: string a; Console.Wri ...

  9. 王立平--string.Empty

    String.Empty 字段 .NET Framework 类库 表示空字符串.此字段为仅仅读.命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) protecte ...

随机推荐

  1. Mybatis逆向工程配置文件详细介绍(转)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  2. [转]console.time和console.timeEnd用法

    console.time和console.timeEnd这两个方法可以用来让WEB开发人员测量一个javascript脚本程序执行消耗的时间.随着WEB应用越来越重要,JavaScript的执行性能也 ...

  3. PHP设置脚本最大执行时间的三种方法

    php.ini 中缺省的最长执行时间是 30 秒,这是由 php.ini 中的 max_execution_time 变量指定,如果脚本需要跑很长时间,例如要大量发送电子邮件,或者分析统计大量数据,服 ...

  4. 执行npm install报错:npm ERR! code EINTEGRITY

    命令行执行npm install报错如下: D:\frontend\viewsdev>npm install npm ERR! code EINTEGRITY npm ERR! sha512-8 ...

  5. GNU Radio 入门培训

    1. GNU Radio介绍 1.1 什么是GNU Radio GNU Radio是一个完全开源的软件无线电结构平台,它可以用来设计和仿真,也可以用来连接真实的无线电系统.GNU Radio是一个高度 ...

  6. SVN checkout 出的项目在PHPstorm中打开没有subversion(SVN)选项按钮怎么办?

    对于svn add 命令的批量操作,为了操作简便还是习惯在IDE中完成,有时候新checkout出的项目,在PHPstorm中右键菜单中没有 Subversion 按钮,操作如下: 点击VCS按钮,然 ...

  7. 【备忘录】Golang交叉编译

    Golang 支持交叉编译,在一个windows平台可以生成linux或Mac系统下的可执行文件. Mac 下编译 Linux 和 Windows 64位可执行程序 CGO_ENABLED=0 GOO ...

  8. 【经验】实现STL算法时遇到的模板编译错误问题

    在实现set_union算法时调用了自己写的copy算法,出现了以下问题. Error 1 error C2665: 'xyz_stl::__copy' : none of the 2 overloa ...

  9. 【原】C++11并行计算 — 数组求和

    本文转载请注明出处 -- polobymulberry-博客园 0x00 - 前言 最近想优化ORB-SLAM2,准备使用并行计算来提高其中ORB特征提取的速度.之前对并行计算方面一窍不通.借此机会, ...

  10. ZOJ 3593 One Person Game(拓展欧几里得求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...