C# 10: Constant Interpolated Strings

A small but welcome quality of life improvement in C# 10. You can finally use string interpolation for `const` strings, as long as the inputs are also strings.

public class Routes
{
    private const string ApiBase = "/api/v1";
    
    // Before C# 10: Compile Error
    // Now: Works!
    public const string Users = $"{ApiBase}/users";
    public const string Orders = $"{ApiBase}/orders";
}

This is fantastic for attributing Controllers or defining consistent routing constants without resorting to `static readonly`.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.