Quick tip: Using format strings with string interpolation in C#

Development General

5 years ago

String interpolation syntax introduced in C# 6 provides a convenient and readable alternative to string.Format. I have used it many times, but only recently have I learned it supports format string syntax as well, which was previously the reason I had to fall back to using string.Format. For example:

string.Format("{0:.#####}", number)

Can be written succinctly using string interpolation as:

$"{number:.#####}"

The syntax used in both cases is analogous - the format string is denoted by a colon (:) character.