String Interpolation in C#

Understanding String Interpolation in C#

**String Interpolation** in C# provides an easy way to format strings by embedding expressions inside **interpolated strings** using the `$` symbol. It enhances readability and eliminates the need for **string concatenation or String.Format()**.

Key Features of String Interpolation

  • Uses **$""** syntax to embed expressions inside strings.
  • Improves **code readability** over traditional string formatting.
  • Supports **expressions, method calls, and calculations** inside strings.
  • Works with **formatting options**, such as numbers and dates.

Basic String Interpolation

Using **$""** allows variables and expressions to be embedded inside strings.

Example: Using String Interpolation with Variables

using System;

class Program
{
    static void Main()
    {
        string name = "Alice";
        int age = 25;

        Console.WriteLine($"Hello, my name is {name} and I am {age} years old.");
    }
}

// Output:
// Hello, my name is Alice and I am 25 years old.
        

The **$""** syntax allows embedding `name` and `age` directly inside the string.

Using Expressions and Method Calls Inside Interpolated Strings

You can **perform calculations** and **call methods** inside interpolated strings.

Example: String Interpolation with Expressions

using System;

class Program
{
    static void Main()
    {
        int a = 10, b = 5;
        Console.WriteLine($"The sum of {a} and {b} is {a + b}");
        Console.WriteLine($"Uppercase: {"hello".ToUpper()}");
    }
}

// Output:
// The sum of 10 and 5 is 15
// Uppercase: HELLO
        

Expressions like `{a + b}` and method calls like `{"hello".ToUpper()}` can be used inside interpolated strings.

Formatting Dates and Numbers in Interpolated Strings

String Interpolation supports **format specifiers** for dates, currency, and decimals.

Example: Formatting Dates and Numbers

using System;

class Program
{
    static void Main()
    {
        DateTime today = DateTime.Now;
        double price = 99.99;

        Console.WriteLine($"Today's date: {today:MMMM dd, yyyy}");
        Console.WriteLine($"Price: {price:C}");
    }
}

// Output:
// Today's date: March 19, 2025
// Price: $99.99
        

**Format specifiers** like `{today:MMMM dd, yyyy}` (long date) and `{price:C}` (currency) are supported in interpolated strings.

Escaping Braces in Interpolated Strings

To print **curly braces** (`{}`) inside an interpolated string, use **double braces** (`{{` and `}}`).

Example: Escaping Curly Braces in Interpolated Strings

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine($"{{This is inside curly braces}}");
    }
}

// Output:
// {This is inside curly braces}
        

Use **double curly braces** `{{` and `}}` to print braces inside an interpolated string.

Best Practices for Using String Interpolation

  • Use **string interpolation instead of string concatenation** for better readability.
  • Use **format specifiers** for numbers, dates, and currency when needed.
  • Avoid **complex logic inside interpolated expressions**—use separate variables for clarity.
  • Use **double braces** (`{{ }}`) when you need to display `{}` in output.