
String interpolation in C# is a feature that allows you to embed expressions within string literals. It provides a more concise and readable way to create strings that include variable values or expressions, compared to concatenating strings with variables.
String interpolation in C#:
Here is an example for string interpolation in C#
Example 1:
In C#, string interpolation is done by prefixing a string literal with the $ symbol and enclosing expressions within curly braces {}. For example:
using System; string name = "Alice"; int age = 30; string message = $"Hello, {name}! You are {age} years old.";
In this example, the values of the name and age variables are inserted into the string message using string interpolation. The resulting string would be: “Hello, Alice! You are 30 years old.”
Example 2:
String interpolation can also be used to format expressions using standard format strings or custom format strings. For example:
using System; double value = 123.45; string formatted = $"The value is: {value:F2}";
In this example, the expression value is formatted as a decimal with 2 decimal places using the F2 format string. The resulting string would be: “The value is: 123.45”
String interpolation is a useful feature that makes it easier to create strings with dynamic content in C#. It helps to improve code readability and reduces the chances of making mistakes when concatenating strings with variables.
Example 3:
You can also include format specifiers within the expressions to format the values. For example:
using System; double price = 19.99; int quantity = 3; string message = $"Total price for {quantity} items is {price * quantity:C2}."; Console.WriteLine(message); // Output: Total price for 3 items is $59.97.
In this example, the price * quantity expression is formatted using the C2 format specifier, which formats the value as a currency with two decimal places.
Example 4:
using System; string name = "John"; int age = 35; string message = $"My name is {name} and I am {age} years old."; Console.WriteLine(message); // Output: My name is John and I am 35 years old.
In this example, the $ symbol before the string literal indicates that string interpolation is being used. The expressions {name} and {age} inside the string are evaluated at runtime and their values are inserted into the string. The resulting string is then printed to the console using Console.WriteLine().
Example 5:
using System; string firstName = "John"; string lastName = "Doe"; int age = 25; string message = $"Hello, my name is {firstName} {lastName}, and I'm {age} years old."; Console.WriteLine(message); // Output: Hello, my name is John Doe, and I'm 25 years old.
In this example, we use string interpolation to embed the firstName, lastName, and age variables within the string message. The values of these variables are evaluated at runtime and replaced with their corresponding values in the final string.
Example 6:
using System; double width = 10.5; double height = 5.2; string message = $"The area of a rectangle with width {width:F2} and height {height:F2} is {width * height:F2}."; Console.WriteLine(message); // Output: The area of a rectangle with width 10.50 and height 5.20 is 54.60.
In this example, we calculate the area of a rectangle using the width * height expression within the string interpolation. We also use the :F2 format specifier to format the width, height, and area values with two decimal places.
– Article ends here –