Does C# 6 string interpolation use boxing like string.Format() does for its arguments?

那年仲夏 提交于 2020-12-05 07:04:26

问题


I am asking this for performance sake - using lots of boxing makes lots of heap allocations which brings more GC collects which sometimes causes apps to freeze for a glimpse which annoy users.


回答1:


All string interpolation does (at least in the common case) is to call string.Format().

Right now, calling string.Format() allocates quite a lot and not just due to boxing (for example, string.Format("{0:s} - {1:B}: The value is: {2:C2}", DateTime.UtcNow, Guid.NewGuid(), 3.50m) makes 13 allocations, only 3 of those due to boxing), though there is talk about improving that in the future.

Though as usual when it comes to performance, you generally should not just blindly write unreadable code everywhere because the readable version has known performance issues. Instead, limit the unreadable efficient code to the parts of your code that actually need it.



来源:https://stackoverflow.com/questions/40133739/does-c-sharp-6-string-interpolation-use-boxing-like-string-format-does-for-its

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!