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

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:


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

Buy me a coffeeBuy me a coffee

3 thoughts on “Quick tip: Using format strings with string interpolation in C#”

  1. String interpolation also works with literal strings:

    var pathname = $@”C:\Temp\Report-{date:yyyy-MM-dd}.txt”

    (Any idea why your comments are forcing everything to upper case? The above was written, in the proper casing, in a text editor and pasted in….)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.