String interpolation in C#


Please contribute by voting. Thanks!

String interpolation is a feature that came with C# 6. It lets you compose strings from text and variables/properties/instances/etc in a simple format. Example:

private string ServiceName => "CustomerService";

public string GetLogString(string errorStr)
    // Text and variables are easily combined
    return $"{ServiceName} | Error: {errorStr}";

You can also do more complex operations as any C# expression that returns a string (or has ToString()) can be included:

var dateStr = $"Current date is {DateTimeOffset.UtcNow.ToString("yyyy-MM-dd")}";

String interpolation replaces String.Format and should have pretty much the same performance, as C# replaces string interpolation to String.Format at compile time.

Article created: Apr 7 at 09:12. Edited Apr 7 at 09:30.

Your comment

You need to sign up / log in to comment this article


Created by Mika [2] Apr 7 at 09:12

Share article

Do you know about


Write an article