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

前端 未结 3 1451
滥情空心
滥情空心 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:28

    ldstr is IL to load a specific string token from metadata.

    ldsfld is IL to load the specified field - which in this case is string.Empty.

    In other words, they're entirely different operations, which happen to have the same result in this case. How are they implemented at the assembly level? Well, that could very well depend on the version of the CLR you're using. Ask your friends which version they're talking about... desktop (32 or 64 bit? 1, 2, 2SP1, 2SP2, 4?), Compact Framework (again, which version?), Silverlight (which operating system, which version?) Did they use cordbg on the code you're actually discussing, or did they do it on some sample code, which may not have been optimized in the same way?

    I would (and have) argued that you should use whichever you find more readable. Personally I prefer "" but others prefer string.Empty. That's fine. Arguing for one over the other on performance reasons requires evidence though... and ideally, evidence based on the code you're actually writing, not a micro-benchmark.

    I would be astonished to see code where any difference between the two actually led to a significant performance difference in real code - other than in situations where there's probably a better way of approaching the task anyway.

提交回复
热议问题