一、string.Empty 和 ""                                                           原文1   原文2

1、Empty是string类中的一个静态的只读字段,它是这样定义的:

// Represents the empty string. This field is read-only.
public static readonly string Empty;

也就是说 string.Empty 的内部实现是等于 "" 的。二者在优化方面稍有差别,string.Empty 是 C# 对 "" 在语法级别的优化。这点可以从上面 string.Empty 的内部实现看出来。也就是说 "" 是通过 CLR(Common Language Runtime)进行优化的,CLR 会维护一个字符串池,以防在堆中创建重复的字符串。而 string.Empty 是一种 C# 语法级别的优化,是在C#编译器将代码编译为 IL (即 MSIL )时进行了优化,即所有对string类的静态字段Empty的访问都会被指向同一引用,以节省内存空间。

PS:MSIL(Microsoft Intermediate Language (MSIL)微软中间语言)。

2、引用类型的数据将对象在堆上的地址保存在"" 都会分配存储空间,具体的说是都会在内存的栈和堆上分配存储空间。因此string.Empty与“”都会在栈上保存一个地址,这个地址占4字节,指向内存堆中的某个长度为0的空间,这个空间保存的是string.Empty的实际值。

测试代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace string_Empty
{
class Program
{
static void Main(string[] args)
{
stringTest();
} public static void stringTest()
{
string str;
string str1;
string str2;
string str3; str = string.Empty;
str1 = string.Empty;
str2 = "";
str3 = "";
}
}
}

Go to disassembly:

可以发现这种写法下,string.Empty 和 "" 的地址是相同的。

由于 string.Empty 定义为 static readonly ,又根据上面运行结果得知, string.Empty 不会申请新的内存,而是每次去指向固定的静态只读内存区域,""也一样。

string.Empty 与 "" 在用法与性能上基本没区别。string.Empty 是在语法级别对 "" 的优化。

二、string.Empty 和 "" 与 null 的区别

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace string_Empty
{
class Program
{
static void Main(string[] args)
{
stringTest();
} public static void stringTest()
{
string str;
string str1;
string str2; str = string.Empty;
str1 = "";
str2 = null;
}
}
}

Go to disassembly:

从运行结果可以看出,string.Empty 和 "" 在栈和堆上都分配了空间,而 null 只在栈上分配了空间,在堆上没有分配,也即变量不引用内存中的任何对象。

C# 中 string.Empty、""、null的差别的更多相关文章

  1. C#中string.Empty ,"" , null 区别

    引言 String类型作为使用最频繁的类型之一,相信大家都非常熟悉,对于string赋予空值,通常有以下三种方式: String str1=null; String str2=””; String s ...

  2. C# 中 string.Empty、""、null的区别

    原文C# 中 string.Empty."".null的区别 一.string.Empty 和 "" 1.Empty是string类中的一个静态的只读字段,它是 ...

  3. C#中string.Empty、""和null 之间的区别

    1.C#中string.Empty.""和null 之间的区别 (http://blog.csdn.net/henulwj/article/details/7830615)

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

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

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

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

  6. C#中String.Empty,“”,NULL的区别

    一.String.Empty String类的静态只读字段.定义如下: public static readonly string Empty; 二.“” 被赋值为“”的字符串变量,会在栈上保存一个地 ...

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

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

  8. C#中string.Empty和""、null的区别

    string.Empty是string类的一个静态常量,而""则表示一个空字符串. string是一种特殊的引用类型,它的null值则表示没有分配内存. 使用ILSpy反编译Str ...

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

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

随机推荐

  1. hdu5432 Pyramid Split

    Problem Description Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which ha ...

  2. VS Code 配置 Java IDE

    背景 维护的项目在一个内网环境,只能通过跳转机的FTP上传文件.项目是Java spring boot开发,之前的维护人员使用sts(https://spring.io/tools),使用起来体验极差 ...

  3. ES中文分词器安装以及自定义配置

    之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了. ik分词 ...

  4. VS常用命令

    1.查看Windows文件首部信息 dumpbin/headers 项目名称. 例如:dumpbin/headers test.exe 2.查看CLR首部信息 dumpbin/clrheader 项目 ...

  5. ArcGIS处理栅格数据(三)

    六.制作镶嵌数据集(栅格数据集优点:a.浏览速度快:b.入库速度快:c.可指定区域显示) 1.右键目录中的数据库,新建"镶嵌数据集". 2.添加栅格数据. 3.定义金字塔. 4.构 ...

  6. npm fetch All In One

    npm fetch All In One fetch for TypeScript { "compilerOptions": { "lib": ["D ...

  7. Promise console.log All In One

    Promise console.log All In One 同步事件/异步事件 微任务/宏任务 js 事件循环原理 先执行 同步事件 在执行,异步事件的所有微任务队列,按照时间顺序 最后执行,异步事 ...

  8. Java IO 通信 All In One

    Java IO 通信 All In One Netty / WebSocket BIO 通信 BIO 即阻塞 I/O,不管是磁盘 I/O 还是网络 I/O,数据在写入 OutputStream 或者从 ...

  9. How to enable HTTPS for local development in macOS using Chrome

    How to enable HTTPS for local development in macOS using Chrome HTTPS, macOS, Chrome local HTTPS htt ...

  10. git config [section] solutions

    git config [section] solutions fix git [section] warnings global config $ vim ~/.gitconfig [user] em ...