String Interpolation vs String.Format

前端 未结 4 881
天命终不由人
天命终不由人 2020-12-02 18:00

Is there a noticable performance difference between using string interpolation:

myString += $\"{x:x2}\";

vs String.Format()?



        
4条回答
  •  渐次进展
    2020-12-02 18:21

    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

提交回复
热议问题