C#

C# String Interpolation

String interpolation in C# allows you to insert words directly into string literals, making it easier to create formatted strings. Or it can be used to write a simple-to-read, simple syntax for forming strings. The string is easier to read than composite formatting.

The '$' character refers to an actual string as an interpolated string. The interpolated string is a string literal containing the interpolation expressions. When the interpolated string is resolved to a result string, the compiler replaces the interpolated strings with the string representation of the expression results.

Example

Let’s see a basic example, to demonstrate the compositing formatting and string interpolation.

csharp
using System; class Program { static void Main(string[] args) { var name = "Aman"; var date = DateTime.Now; // Composite Formatting Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date); // String interpolation Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now."); } }

Output

Hello, Aman! Today is Saturday, it's 18:34 now.
Hello, Aman! Today is Saturday, it's 18:34 now.

Structure of an interpolated string

To identify a string literal as an interpolated string, consider it with the '$' symbol. We can't have any white space between the '$' and the " that starts a string literal.

The structure of an item with an interpolation expression is as follows:

csharp
{<interpolationExpression>[,<alignment>][:<formatString>]}

Key Features of String Interpolation

The following are the key features of string interpolation:

Embedded Expressions: We can include any valid C# expression within the curly braces. For example:

csharp
int x = 10; int y = 20; string result = $"The sum of {x} and {y} is {x + y}."; Console.WriteLine(result); // Output: The sum of 10 and 20 is 30.

Formatting: We can also format numbers, dates, and other types directly within the interpolation:

csharp
double price = 9.99; string formattedPrice = $"The price is {price:C}."; // Formats as currency Console.WriteLine(formattedPrice); // Output: The price is $9.99.

Multiline Strings: We can use interpolated strings in multiline contexts:

csharp
string name = "Alice"; string greeting = $@"Hello, {name}, Welcome to our program!"; Console.WriteLine(greeting);

Escaping Curly Braces: If we need to include literal curly braces in our string, we can escape them by doubling them:

csharp
string message = $"This will show a brace: {{ and }}"; Console.WriteLine(message); // Output: This will show a brace: { and }

Performance

String interpolation is generally efficient and often preferred for readability. However, consider using StringBuilder for better performance if you're doing many concatenations in a loop.

In summary, String interpolation provides a clean and concise way to construct strings in C#. It benefits structured threads with dynamic content, increasing code readability and maintainability.

line

Copyrights © 2024 letsupdateskills All rights reserved