一、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. Codeforces Round #604 (Div. 2) B. Beautiful Numbers(双指针)

    题目链接:https://codeforces.com/contest/1265/problem/B 题意 给出大小为 $n$ 的一个排列,问对于每个 $i(1 \le i \le n)$,原排列中是 ...

  2. hdu5643 King's Game(约瑟夫环+线段树)

    Problem Description In order to remember history, King plans to play losephus problem in the parade ...

  3. fzu2198 快来快来数一数

    Accept: 204    Submit: 627 Time Limit: 1000 mSec    Memory Limit : 65536 KB  Problem Description n个六 ...

  4. Checkout Assistant CodeForces - 19B

    题意: 给你n个物品,每个物品有一个价格ci和一个支付时间ti,在这个ti时间内,你可以免费拿ti个物品.问你想要带走这n个物品最小需要多少钱 题解: 原本还想着贪心去写,但是好像贪心写不了,,,不属 ...

  5. PowerShell随笔5---添加.NET类型

    有些情况下,有些脚本命令不能满足我们的需求,而手头却能用C#很方便的实现. 我们就可以把自定义的类型Add到PowerShell中使用,使用方法和PowerShell调用.NET类库方法是一样的. 以 ...

  6. 实现基于股票收盘价的时间序列的统计(用Python实现)

    时间序列是按时间顺序的一组真实的数字,比如股票的交易数据.通过分析时间序列,能挖掘出这组序列背后包含的规律,从而有效地预测未来的数据.在这部分里,将讲述基于时间序列的常用统计方法. 1 用rollin ...

  7. Error Code: 1055.Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.

    环境:mysql-8.0.15-winx64 问题描述: Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expre ...

  8. C# 类 (12) - Partial

    Partial 前面说了,同一个namespace 里 class 名字是不能重的,除非是在不同的namespace里,下面开始打脸在同一个namespace里,加上partial 关键字,可以写同样 ...

  9. Bootstrap微章

    给链接.导航等元素嵌套 span class="badge" 元素,可以很醒目的展示新的或未读的信息条目. <a href="#">Inbox &l ...

  10. java之 javassist简单使用

    0x01.javassist介绍 什么是javassist,这个词一听起来感觉就很懵,对吧~ public void DynGenerateClass() { ClassPool pool = Cla ...