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:

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.

2 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 class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*

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