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


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

    一、string.Empty 和 ""

    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 = "";  
                }  
            }  
        }  
    

     设置断点后运行结果如下:


     
            可以发现这种写法下,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;  
                }  
            }  
        }  
    

        设置断点后运行结果如下:


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

  • 相关阅读:
    python 数据可视化(一)
    python unittest自动测试框架
    使用selenium抓取淘宝信息并存储mongodb
    python之re模块(正则表达式)
    [原] OpenGL ES 学习笔记 (二)
    [原] OpenGL ES 学习笔记 (一)
    [转] iOS开发同学的arm64汇编入门
    [转]ARM64 汇编
    [转]iOS高级调试&逆向技术-汇编寄存器调用
    [转] CGTime CMTimeRange CMTimeMapping 小结
  • 原文地址:https://www.cnblogs.com/arxive/p/5948290.html
Copyright © 2020-2023  润新知