C# tutorials > Modern C# Features > C# 6.0 and Later > What are constant interpolated strings?
What are constant interpolated strings?
Constant interpolated strings are a feature introduced in C# 6.0 that allows you to create string constants using string interpolation, provided that the interpolated values are also constants or compile-time constants. This feature enables you to embed constant values directly into a string at compile time, improving performance and readability. They are declared using the const
keyword in conjunction with an interpolated string that utilizes only compile-time constants.
Basic Syntax
In this example, name
and age
are constant variables. The message
is a constant interpolated string that combines these constants at compile time. The resulting string is also a constant.
const string name = "Alice";
const int age = 30;
const string message = $"Name: {name}, Age: {age}";
Console.WriteLine(message); // Output: Name: Alice, Age: 30
Concepts Behind the Snippet
The key concept here is compile-time evaluation. The C# compiler evaluates the interpolated string at compile time, replacing the placeholders with their constant values. This means that there's no runtime overhead for string formatting. The resulting string is stored directly in the assembly's metadata, just like any other string literal. This is significantly different from regular string interpolation where the string is constructed during runtime.
Real-Life Use Case Section
Consider scenarios where you need to define fixed configuration values or error messages. For example, you might use constant interpolated strings to define database connection strings with known server names, or version information.
const string ServerName = "localhost";
const string DatabaseName = "MyDatabase";
const string ConnectionString = $"Server={ServerName};Database={DatabaseName};Integrated Security=true;";
Console.WriteLine(ConnectionString);
Best Practices
string.Format
.
Interview Tip
When discussing constant interpolated strings in an interview, emphasize the compile-time evaluation aspect and the performance benefits derived from it. Also, highlight the constraint that only constant values can be used within the interpolated string. Be prepared to compare them to regular string interpolation and string.Format().
When to Use Them
Use constant interpolated strings when:
Memory Footprint
Constant interpolated strings do not create new string objects at runtime. The resulting string is stored as a literal in the assembly's metadata. Therefore, they can be more memory-efficient than building strings at runtime, especially if the string is used frequently.
Alternatives
Alternatives to constant interpolated strings include:
Pros
Cons
FAQ
-
What happens if I try to use a non-constant variable in a constant interpolated string?
You will get a compile-time error. The compiler requires that all values within a constant interpolated string be compile-time constants. -
Can I use methods or properties within a constant interpolated string?
No, you cannot use methods or properties directly because they are typically evaluated at runtime. Only constant values are allowed. -
Are constant interpolated strings interned?
Yes, like other string literals in C#, constant interpolated strings are interned. String interning is a process where the common language runtime (CLR) maintains a table (called the intern pool) to store unique string literals. If the same string literal is encountered multiple times in the code, the CLR will reuse the existing string object from the intern pool rather than creating new ones, thus saving memory.