Constant Interpolation in C# 10 and .NET 6 In 10 Minutes or Less
With each new version, C# continues to evolve in both power and elegance, offering developers cleaner and more efficient ways to write code. One such addition introduced in C# 10 is constant interpolated strings — a feature that allows developers to use string interpolation inside constants.
Before this feature, if you tried to create a constant string using interpolation (for example, $"{companyName} Products"), the compiler would throw an error. You could only assign plain string literals to const values, which often led to repetitive or clunky code when building constant messages or attribute strings.
Now, with C# 10 and .NET 6 onwards, you can combine constants together using interpolated strings directly at compile time. This enhancement not only makes your code cleaner but also keeps it efficient, as all values are evaluated before the program runs.
To get a clearer understanding of how this works, Tim Corey, in his video “Constant Interpolation in C# 10 and .NET 6 in 10 Minutes or Less,” walks us through the concept step by step. In his usual concise and practical teaching style, Tim explains what this new feature does, how it differs from traditional string concatenation, and where it can be especially useful — particularly in attributes.
So let’s go through Tim’s explanation to see exactly how constant interpolated strings in C# work and why they’re a helpful addition for developers.
Introduction
In this video, Tim explains how interpolated strings — those using the familiar dollar sign ($) syntax — can now be used in constant expressions, something that was not possible before. This small change makes constant string construction easier, cleaner, and more maintainable.
Exploring Constants in .NET 6
Tim opens a .NET 6 console application and removes the boilerplate code to focus purely on constant definitions.
He begins with the most basic constant string declaration:
const string companyName = "Acme";const string companyName = "Acme";This is a simple string literal assigned to a const string. Constants like this are evaluated at compile time, which means their values are fixed and embedded into the compiled program.
But Tim quickly moves to the central question: what if we want to merge constant strings together or embed values directly inside other strings using interpolation?
In C# 9 and earlier, you couldn’t do this:
const string productName = $"{companyName} Anvils"; // Not allowed before C# 10const string productName = $"{companyName} Anvils"; // Not allowed before C# 10This line would throw a compile-time error, since string interpolation wasn’t supported for constant expressions.
Constant Interpolated Strings in C# 10
As Tim demonstrates, in C# 10, the compiler now supports constant interpolated strings — as long as all interpolation expressions inside are themselves constants.
So the following example now works perfectly:
const string productName = $"{companyName} Anvils";const string productName = $"{companyName} Anvils";This is a constant interpolated string, meaning the compiler evaluates the interpolated string at compile time instead of runtime. No extra string concatenation or string formatting happens when the program runs — the compiler generates a single constant string literal like "Acme Anvils".
Tim explains that if we change the value of companyName from "Acme" to "ABC", the compiler automatically generates "ABC Anvils" for productName. It’s compile-time string construction, not runtime interpolation.
This enhancement makes it much easier to merge constant strings without resorting to + concatenation or manually repeating values.
Nested Constant Interpolation
Tim goes one step further with another constant definition:
const string productDescription = $"{productName} are the best way to crush unsuspecting roadrunners.";const string productDescription = $"{productName} are the best way to crush unsuspecting roadrunners.";This is an example of nested interpolation, where one constant interpolated string (productName) is used inside another.
The compiler treats all these as constant expressions, generating a single, immutable string representation at compile time.
When Tim runs the program, the output reads:
Acme Anvils are the best way to crush unsuspecting roadrunners.Acme Anvils are the best way to crush unsuspecting roadrunners.This confirms that constant interpolation works seamlessly, even across multiple constants.
Why Constants Matter
At this point, Tim pauses to explain why constants — and now constant interpolated strings — are beneficial.
He points out that constants are extremely memory efficient because their values are stored directly in the compiled code, not as separate instances in memory.
In contrast, when developers previously needed something similar, they often used readonly fields:
readonly string companyName = "Acme";readonly string companyName = "Acme";But Tim notes that readonly fields are not the same as const — they are evaluated at runtime, which consumes more memory and prevents compile-time optimization.
With constant interpolated strings, we can now write expressive and reusable formatted strings that remain compile-time constants, improving both clarity and performance.
Practical Example — Using Constants in Attributes
Tim then introduces a real-world scenario where this new feature shines — attributes.
He defines a simple local method inside Main():
void SayHi() { }void SayHi() { }Then he tries to apply the [Obsolete] attribute with a string message that references a variable:
string myCompany = "Tim's Company";
[Obsolete($"This is no longer used for {myCompany}")]string myCompany = "Tim's Company";
[Obsolete($"This is no longer used for {myCompany}")]This fails because attributes can only accept constant expressions as parameters. The compiler generates an error since myCompany is a variable, not a constant.
Tim explains that the attribute’s message needs to be compile-time constant — it cannot depend on runtime values or instance variables.
However, thanks to constant interpolated strings in C# 10, we can now do this safely:
const string productName = $"{companyName} Anvils";
[Obsolete($"This is no longer used for {productName}")]const string productName = $"{companyName} Anvils";
[Obsolete($"This is no longer used for {productName}")]Here, the compiler recognizes that both companyName and productName are constants, so the entire interpolated string is a constant expression.
The compiler generates the formatted string at compile time, making it valid inside the attribute.
This example perfectly highlights why constant interpolation is not just syntactic sugar — it enables new scenarios like using formatted compile-time strings directly in attributes or metadata.
Behind the Scenes — How the Compiler Handles It
Tim doesn’t go into low-level compiler internals in the video, but the concept ties closely to how interpolated string handlers work in C# 10.
In general, when the compiler encounters an interpolated string, it creates code similar to a format string operation, generating calls like AppendLiteral() and AppendFormatted() behind the scenes.
But when dealing with constant interpolated strings, the compiler evaluates everything at compile time — no interpolated string handler or method call is emitted in the generated IL code.
This means the resulting value behaves exactly like any string literal, but you can still compose it using embedded expressions from other constants.
It’s an elegant balance between expressiveness and efficiency — the compiler handles the string construction statically, ensuring zero runtime cost.
When to Use Constant Interpolated Strings
Tim acknowledges that not everyone will use this feature every day. Developers who rarely define constants or write attributes may not immediately benefit.
However, for those who create many compile-time definitions, constant messages, or attribute metadata, this feature simplifies code and prevents string concatenation clutter.
It’s also safer — because constants are immutable and checked by the compiler, you eliminate errors from dynamically concatenated strings or mismanaged variables.
This makes your code more robust, readable, and easier to maintain.
Conclusion
As Tim wraps up his video, he invites developers to reflect on whether they’ll use constant interpolated strings in their projects. Some might find them essential for cleaner compile-time formatted strings, while others may see them as a minor convenience.
Either way, Tim’s demonstration shows exactly how to implement and where to apply this feature effectively.
In summary:
Constant interpolated strings allow interpolation expressions within constants.
They’re evaluated at compile time, producing efficient string literals.
They replace repetitive string concatenation with cleaner syntax.
- They’re particularly useful in attributes, constants-based configuration, and metadata messages.
By combining readability with compile-time safety, C# 10’s constant interpolation is another step toward expressive and efficient C# programming — just as Tim Corey’s example clearly demonstrates.
