What's the different between ldsfld and ldstr in IL?

前端 未结 3 1449
滥情空心
滥情空心 2021-01-05 03:07

I\'ve read some article about String.Empty vs \"\" and I also do test by my self. Different between them are below.

String.Empty

L_0001: ldsfld strin         


        
3条回答
  •  悲哀的现实
    2021-01-05 03:25

    From .NET 4.5 the difference in this case is: exactly nothing. It looks like the JIT now detects this ldsfld and injects the interned empty string directly.

    You can tell this because in < 4.5, you can change the value of string.Empty via reflection and it impacts code using string.Empty. Nasty but possible. From 4.5 this no longer works. If you can check via reflection you get the hacked version, but code using string.Empty via ldsfld gets the correct empty string rather than the hacked version.

提交回复
热议问题