一、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. 1155 Heap Paths

    题干前半略. Sample Input 1: 8 98 72 86 60 65 12 23 50   Sample Output 1: 98 86 23 98 86 12 98 72 65 98 72 ...

  2. Dire Wolf——HDU5115

    Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not ...

  3. MIT 6.S081 聊聊xv6的文件系统(中)日志层与事务

    前言 我本想把上篇中没讲完的剩余层全部在本篇中讲完,但没想到越写越多.日志层的代码不多,其思想和解决问题的手段也不算难以理解,但其背后涉及的原理和思想还是非常值得回味的,因此我打算用一整篇完整的blo ...

  4. C#Assembly、程序集、装配件、命名空间以及类型的关系

    Assembly = 程序集 = 装配件 命名空间是类的逻辑组织形式,程序集是类的物理组织形式. 程序集其实和命名空间没有什么必然的联系. 程序集1: namespace1{ public class ...

  5. 11.PowerShell DSC之安装PowerShell Module

    打开https://powershellgallery.com,检索你需要的目标模块,我们以安装名为"xmysql"的module为例: 自动安装 1.执行命令install-mo ...

  6. 004、Python xlsxwriter模块

    简单用法demo # !/usr/bin/python # coding:utf-8 # xlsxwriter的基本用法 import xlsxwriter # 1. 创建一个Excel文件 work ...

  7. MySQL 企业案例:误删核心业务表

    问题描述: 1.正在运行的网站系统,MySQL 数据库,数据量 25G,日业务增量 10 - 15M 2.备份策略:每天 23:00,计划任务调用 mysqldump 执行全备脚本 3.故障时间点:上 ...

  8. sizeof和strlen在string类中的使用

    字符串的sizeof和strlen 考虑下面的问题: char a[] = "abcdef"; char b[20] = "abcdef"; string s ...

  9. 手工数据结构系列-C语言模拟队列 hdu1276

    #include <stdio.h> #include <stdlib.h> #define init_size 1000 typedef struct { int head, ...

  10. 5种设置ASP.NET Core应用程序URL的方法

    默认情况下,ASP.NET Core应用程序监听以下URL: http://localhost:5000 https://localhost:5001 在这篇文章中,我展示了5种不同的方式来更改您的应 ...