Skip to content

Constant interpolated strings C# 10.0readabilitycode reduction

Allow constant strings to use string interpolation.

String interpolation C# 6.0 allows strings to be built up using a syntax from inside the string itself rather than joining variables together with + which can reduce the noise in many scenarios.

C# 10.0 finally allows you to use this feature for constant strings as well, provided every placeholder is itself a constant string.

Code ​

csharp
const string Scheme = "https";
const string Host = "api.example.com";
const string BaseUrl = $"{Scheme}://{Host}/v1";
csharp
const string Scheme = "https";
const string Host = "api.example.com";
const string BaseUrl = Scheme + "://" + Host + "/v1";

Notes ​

  • Numeric constants are not allowed as placeholders because formatting them depends on the current culture
  • nameof produces a constant string so it works too, which is handy in attributes such as [Obsolete($"Use {nameof(SendAsync)} instead")]

More information ​

Powered by Intracia