String Interpolation vs String.Format

旧时模样 提交于 2019-11-27 12:15:51
Jeroen Vannevel

Noticable is relative. However: string interpolation is turned into string.Format() at compile-time so they should end up with the same result.

There are subtle differences though: as we can tell from this question, string concatenation in the format specifier results in an additional string.Concat() call.

string interpolation is turned into string.Format() at compile-time.

Also in string.Format you can specify several outputs for single argument, and different output formats for single argument. But string interpolation is more readable I guess. So, it's up to you.

a = string.Format("Due date is {0:M/d/yy} at {0:h:mm}", someComplexObject.someObject.someProperty);

b = $"Due date is {someComplexObject.someObject.someProperty:M/d/yy} at {someComplexObject.someObject.someProperty:h:mm}";

There is some performance test results https://koukia.ca/string-interpolation-vs-string-format-string-concat-and-string-builder-performance-benchmarks-c1dad38032a

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