Why do the overloads of String.Format exist?

后端 未结 2 785
逝去的感伤
逝去的感伤 2021-01-07 20:25

I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 argu

相关标签:
2条回答
  • 2021-01-07 21:06

    You are forgetting about the code in the app required to make the call. Creating the array and filling it takes a lot more IL than just passing 3 args.

    0 讨论(0)
  • 2021-01-07 21:25

    One reason, as Hans mentions, is that creating an array is a lot of unnecessary overhead in most common cases of formatting a string. This saves space in the EXE.

    Another reason is that not all languages support variadic functions (use of params in C#). This allows users of those languages to avoid array creation for the most common cases of string formatting. This saves a lot for languages that don't have simple syntax for array creation and initialization.

    0 讨论(0)
提交回复
热议问题