C# 中 string.Empty、""、null的差别
一、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的差别的更多相关文章
- C#中string.Empty ,"" , null 区别
引言 String类型作为使用最频繁的类型之一,相信大家都非常熟悉,对于string赋予空值,通常有以下三种方式: String str1=null; String str2=””; String s ...
- C# 中 string.Empty、""、null的区别
原文C# 中 string.Empty."".null的区别 一.string.Empty 和 "" 1.Empty是string类中的一个静态的只读字段,它是 ...
- C#中string.Empty、""和null 之间的区别
1.C#中string.Empty.""和null 之间的区别 (http://blog.csdn.net/henulwj/article/details/7830615)
- string.empty , "" , null 以及性能的比较
一:这种结论,个人觉得仍然存疑 http://www.cnblogs.com/wangshuai901/archive/2012/05/06/2485657.html 1.null null 关 ...
- String.Empty,NULL和""的区别
String.Empty,NULL和""的区别 string.Empty就相当于"" 一般用于字符串的初始化 比如: string a; Console.Wri ...
- C#中String.Empty,“”,NULL的区别
一.String.Empty String类的静态只读字段.定义如下: public static readonly string Empty; 二.“” 被赋值为“”的字符串变量,会在栈上保存一个地 ...
- asp.net(c#)中String.Empty、NULL、"" 三者到底有啥区别和联系?
开门见山,首先看下面代码,你认为结果分别是什么? string str = string.Empty; string str1 = ""; string str2 = null; ...
- C#中string.Empty和""、null的区别
string.Empty是string类的一个静态常量,而""则表示一个空字符串. string是一种特殊的引用类型,它的null值则表示没有分配内存. 使用ILSpy反编译Str ...
- C#中String.Empty、NULL与""三者的区别
String.Empty和""是一样的,都是空,习惯用string.empty. Null和他们就有区别了,就是没有值,也没分配地址,此处可以理解成什么都没有.
随机推荐
- FatMouse and CheeseI - I
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- 【noi 2.6_7627】鸡蛋的硬度(DP)
题意:其中n表示楼的高度,m表示你现在拥有的鸡蛋个数. 解法:f[i][j]表示 i 层楼有 j 个鸡蛋时,至少要扔多少次.3重循环,k为测试的楼层,分这时扔下去的鸡蛋碎和不碎的情况.要注意初始化. ...
- 牛客编程巅峰赛S2第10场 - 钻石&王者 C.牛牛的路径和 (位运算,dfs)
题意:给你节点数为\(n\)的树,每个节点都有自己的权值,求所有路径的上的点的权值按位与的和. 题解:题目给的数据很大,我们不能直接去找.因此我们可以枚举二进制\([1,20]\)的每一位,然后再枚举 ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- C# 网络流
流(stream)是对串行传输的数据的一种抽象表示,底层的设备可以是文件.外部设备.主存.网络套接字等等. 流有三种基本的操作:写入.读取和查找. 如果数据从内存缓冲区传输到外部源,这样的流叫作&qu ...
- VScode 配置c++环境
参考 https://code.visualstudio.com/docs/cpp/config-mingw https://zhuanlan.zhihu.com/p/77645306 主要 http ...
- Invalid prop: custom validator check failed for prop "***"
使用element ui时,绑定参数参考官方参数说明,比如progress的status 值必须是在以下三个里面选择 "success/exception/text", 其它情况可 ...
- how to read the 10th line of a text using shell script
how to read the 10th line of a text using shell script shell script / bash script question https://l ...
- how to using Linux pipe command output another command's help content to a file
how to using Linux pipe command output another command's help content to a file Linux tee > >& ...
- 如何在 macOS 上进行滚动截屏
如何在 macOS 上进行滚动截屏 Shift-Command-5 https://support.apple.com/zh-cn/guide/mac-help/mh26782/mac demo Xn ...