4 ms·
string interpolation isn't simply a wrapper around String.Format(); it also offers compile time checking which solved a real limitation of String.Format() while
by Someone1234 8y ago
string interpolation isn't simply a wrapper around String.Format(); it also offers compile time checking which solved a real limitation of String.Format() while offering better performance than concatenation.
For example:
var h = "Hello ";
var w = "world";
var output = String.Format("{1} {2}", h, w);
output = $"{h} {w}";
Would compile fine but throw on the String.Format() line due to the typo.
- chrismeller 8y agoI wasn’t necessarily saying it doesn’t offer any benefit, more that it’s not really a benefit that seems to benefit a lot of people. Between code intelligence tools and actually running the code you write (not even in an actual QA process, but simply executing each line) that just doesn’t seem like a big deal to me. There are also instances where, such as with Console.Write you are now formatting a string in order to pass it into... a method that formats strings. Yes, the same benefit is still provided, but with a fairly obvious decrease in clarity and increase in redundancy.
- Someone1234 8y ago> fairly obvious decrease in clarity You feel that "{0} {1} {2}" provides better clarity than $"{FirstName} {MiddleInitial} {LastName}"?
- EpicEng 8y ago>a fairly obvious decrease in clarity In what's ways specifically? Using String.Format you have to keep your indices correct and 'compiling' the string in your head requires jumping back and forth between the format string and the args. Interpolation wins on every account as far as I'm concerned.