Why does interpolating a const string result in a compiler error?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, an interpolated string ($"Hello, {name}") cannot be assigned to a const string because the compiler evaluates const values at compile time, and interpolated strings are converted to string.Format() or string.Concat() calls that execute at runtime. Constants can only be initialized with compile-time constant expressions — literal values, other constants, or constant operations. Starting with C# 10, interpolated strings of constant expressions can be const, but only if every interpolated value is itself a constant.
The Error
Before C# 10, all interpolated strings were treated as runtime expressions, even if every placeholder contained a constant. C# 10 added support for constant interpolated strings when all interpolated values are themselves const.
Why Interpolation Was Not Constant (Pre-C# 10)
The compiler did not evaluate interpolated strings at compile time. Even $"Hello, {"World"}" was treated as a method call, disqualifying it from const assignment.
C# 10: Constant Interpolated Strings
In C# 10+, the compiler evaluates constant interpolated strings at compile time if every {} expression resolves to a const value.
Workarounds for Pre-C# 10
String concatenation with + on constant strings was always evaluated at compile time, making it the standard workaround.
const vs static readonly
| Feature | const | static readonly |
| Evaluation | Compile time | Runtime (once) |
| Interpolation (C# 10+) | Only with const values | Any expression |
| Can use runtime values | No | Yes |
| Stored in metadata | Yes (inlined) | No (field reference) |
| Changes require recompile | All referencing assemblies | Only declaring assembly |
When const Matters
Common Pitfalls
- Assuming interpolation is always non-constant: In C# 10+,
$"Hello, {constValue}"is a valid constant expression ifconstValueis itselfconst. Check your language version before applying workarounds that may not be needed. - Using
static readonlywhenconstis required: Attribute arguments, switch case labels, and default parameter values require compile-time constants.static readonlyis not a compile-time constant and cannot be used in these contexts. - Forgetting that
constvalues are inlined: When aconstvalue from Assembly A is used in Assembly B, the value is copied into Assembly B at compile time. Changing the const in A without recompiling B leaves B with the old value. Usestatic readonlyfor values that may change between releases. - Mixing const and non-const in interpolation (C# 10+):
$"{constPart}{nonConstPart}"is not constant because one expression is notconst. All interpolated values must be const for the entire expression to be const. - Interpolating non-string types in const context:
const int x = 42; const string s = $"{x}";does not work even in C# 10 becauseint.ToString()is a method call. Onlyconst stringinterpolation values are allowed in constant interpolated strings.
Summary
- Interpolated strings cannot be
constbefore C# 10 because they compile tostring.Format()orstring.Concat()calls - C# 10+ allows
constinterpolated strings when all interpolated values are themselvesconst string - For pre-C# 10, use
+concatenation with const strings instead of interpolation - Use
static readonlyinstead ofconstwhen you need runtime expressions in string construction constvalues are inlined at compile time — changes require recompiling all referencing assemblies- Attribute arguments, switch labels, and default parameters require true compile-time constants

