C#关于String.Empty是怎么初始化的?

C#关于String.Empty是怎么初始化的?

C#中String类的源码在这里:

https://referencesource.micro...

我知道String.Empty == ""是成立的,但是想知道String.Empty是怎么定义的
源码里边是这样

public static readonly String Empty;

但是也没看到初始化给它赋值呀.

记得以前的旧版本源码是直接赋值的"",但是这个我不确定在哪里给他的初始值,

求解释...

阅读 2.3k
评论
    2 个回答

    擅自源代码的注释:

        
        // The Empty constant holds the empty string value. It is initialized by the EE during startup.
        // It is treated as intrinsic by the JIT as so the static constructor would never run.
        // Leaving it uninitialized would confuse debuggers.
        //
        //We need to call the String constructor so that the compiler doesn't mark this as a literal.
        //Marking this as a literal would mean that it doesn't show up as a field which we can access 
        //from native.
      意思是说初始化由JIT保证,无需手动赋值。

      事实上并不需要赋值,因为不显式负值则在运行时会给一个默认值。int 类型的是0,String类型的就是空字符串

        撰写回答

        登录后参与交流、获取后续更新提醒

        相似问题
        推荐文章