Formatting interpolated strings

How interpolated strings can be formatted without using the ToString method

Home DailyDrop

Daily Knowledge Drop

When using string interpolation, the colon : operator can be used, followed by the format string to specify how the string should be formatted. This can be used instead of the ToString method with a specified format.

This post is about interpolated strings however the technique will also work when using the index component (using String.Format with {0} instead the string).


Format

In a previous post, we learnt how to align the string when performing string interpolation - but when using this method (and other composite string techniques) there also an optional formatString component.

The full syntax for:

  • string interpolation is: {<interpolationExpression>[,<alignment>][:<formatString>]}
  • composite formatting is: {index[,alignment][:formatString]}

This post will focus on the formatString portion - this provides a shortcut to using the .ToString(format) method on the relevent entity.


Examples

In the examples below, the ToString method, and the interpolated format string method are compared, with the same format string, and shown to produce the same output. It is also shown when no format string is specified, the `general ("G") format specifier is used (for numeric, datetime and enumeration).

DateTime

Below we see how to format a DateTime using the more traditional ToString method, and then the format string method:

DateTime current  = DateTime.Now;
Console.WriteLine($"Current Datetime:{current}");
Console.WriteLine($"Current Datetime:{current.ToString("MM/dd/yyyy hh:mm:ss.fff")}");
Console.WriteLine($"Current Datetime:{current:MM/dd/yyyy hh:mm:ss.fff}");

Console.WriteLine($"Current Datetime:{current.ToString("hh:mm")}");
Console.WriteLine($"Current Datetime:{current:hh:mm}");

Executing the above:

Current Datetime:2022/08/15 05:56:53
Current Datetime:09/07/2022 05:54:25.527
Current Datetime:09/07/2022 05:54:25.527
Current Datetime:05:54
Current Datetime:05:54

Same output with the two methods, but when using the interpolated format string method, the code is more concise and (arguably) cleaner.


Guid

A Guid example:

Guid newGuid = Guid.NewGuid();
Console.WriteLine($"Guid value: {newGuid}");
Console.WriteLine($"Guid value: {newGuid.ToString("B")}");
Console.WriteLine($"Guid value: {newGuid:B}");

Executing the above:

Guid value: bcaf5b5e-14be-4156-a480-a87614a900f4
Guid value: {bcaf5b5e-14be-4156-a480-a87614a900f4}
Guid value: {bcaf5b5e-14be-4156-a480-a87614a900f4}

TimeSpan

A TimeSpan example:

TimeSpan travelTime = new TimeSpan(1, 6, 24, 1);
Console.WriteLine($"Travel time: {travelTime}");
Console.WriteLine($"Travel time: {travelTime.ToString("g")}");
Console.WriteLine($"Travel time: {travelTime:g}");

Executing the above:

Travel time: 1.06:24:01
Travel time: 1:6:24:01
Travel time: 1:6:24:01

Numeric

A numeric example:

int cost = 5699;
Console.WriteLine($"Cost: {cost}");
Console.WriteLine($"Cost: {cost.ToString("c")}");
Console.WriteLine($"Cost: {cost:c}");

Executing the above:

Cost: 5699
Cost: R5 699,00
Cost: R5 699,00

Enum

A Enum example:

ConsoleColor drawColor = ConsoleColor.Green;
Console.WriteLine($"Draw color: {drawColor}");
Console.WriteLine($"Draw color: {drawColor.ToString("D")}");
Console.WriteLine($"Draw color: {drawColor:D}");

Executing the above:

Draw color: Green
Draw color: 10
Draw color: 10

Notes

This is not a new or revolutionary feature. I tend to use the ToString method when formatting strings, not really aware that there was an alternative. Using the :formatString method is not going to drastically change the maintainability, readability or performance of the code - but it does require slightly less typing and removes one additional method call (ToString) so personally I will be using this method more frequently going forward.


References

Composite formatting: Format string component

Daily Drop 156: 08-09-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net interpolation formatting string