Why does interpolating a const string result in a compiler error?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Related reading
- Why does ListT implement IReadOnlyListT in .NET 4.5?
- Why does Math.Round2.5 return 2 instead of 3?
- Why does my .NET Standard NuGet package trigger so many dependencies?
- Why does nameof return only last name?
- Why does kafka streams threads die when the source topic partitions changes ? Can anyone point to reading material around this?
- Why does kubectl cp command terminates with exit code 126?
- Why does .NET foreach loop throw NullRefException when collection is null?
- Why does .NET use banker's rounding as default?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.