1 hour ago · 13 min read2615 words · Tech · hide

In this post, I describe how you can use a custom [InterpolatedStringHandler] to ensure string interpolation (e.g. $"My value is {value}") formats values using the invariant culture. This can be important in many situations, such as in logging and other telemetry, as well as if you need to round trip values between different formats. Interpolated strings and culture formatting String interpolation was introduced way back in C# 6, back when .NET Core 1.0 was just being released! In it's simplest form, string interpolation provides some syntactic sugar around the venerable string.Format() method. So instead of code that looks like this: var someInt = 123; var someDouble = -1.23d; string formatted = string.Format("the int is: {0}, the double is: {1}, someInt, someDouble"); The string formatting code instead looks like this: string formatted = $"the int is: {someInt}, the double is: {someDouble}"; As well as being neater and easier to follow, it also removes a class of bugs related to…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town