C# String Interpolation, and Expressions, used in a way I have never encountered before

When reviewing other developers’ code, I often see small constructs that I would never write myself, yet pause thinking for a moment, “I must remember that, it’s quite neat”.

This happened a few moments ago upon reviewing some third-party code. I encountered a small construct that appeared to be a simple piece of stock standard C# string interpolation. Documentation on string interpolation, including the reference documentation from Microsoft and Jon Skeet’s very addictive blog and books, all routinely demonstrate string interpolation as an alternative to the static String.Format(…) method. Furthermore a rote discussion on string interpolation functionality during a technical interview would most certainly result in someone at some point referring to String.Format, syntactic sugar with StringBuilder efficiency in terms of performance and GC issues, and so on.

The code excerpt below shows a simple implementation of string expression interpolation that I had never encountered before; prefix addition within the string with the side effect that the state of an in-scope variable is updated.

This approach shown below is succinct, unusual, yet obvious too. That is a very odd mixture!

This is one of my shorter blog postings but then again, what else is there to write?

— Published by Mike, 09:06:07 25 September 2017 (GMT)

Leave a Reply