C#

What is string interpolation in C#

Interpolating expression values into literal strings is possible with the technique known as string interpolation. String interpolation feature was introduced in C# 6. A string is considered as an interpolated string if it contains the interpolation expression.

Using string interpolation in C# is the subject of this article. String is identified as an interpolated string in C# by the special character $.

Variable expansion, variable interpolation, and variable substitution are other terms for this technique. String literals containing one or more placeholders that are replaced by corresponding values are evaluated in this process.

Python, Perl, PHP, Ruby, Java, Scala, and many other programming languages support string interpolation.

The reason for utilizing an interpolated string is that it is easier to do the string concatenation without having to format it.

Previously, we were using formatted strings instead of interpolation.

Let’s look at an example of a formatted string and an interpolated string.

string fistName = "Rajeev";

string lastName = "Kumar";

Using the string formatting option we can write,

Console.WriteLine("Hello {0} {1}", fistName, lastName);

C# String Interpolation

But by using the string interpolation feature we can write as below,

Console.WriteLine($"Hello {fistName} {lastName}");

Both the above instructions will produce the same output as given below,

Hello Rajeev Kumar

The interpolated string starts with $” and enclose variable names in curly braces “{ }”.

The interpolated string has a three part syntax. They are, Interpolation Expression, Alignment, and FormatString.

Interpolation Expression

We have seen in the above example which is enclosed by curly braces “{ }”.

The alignment will define the minimum no of characters in the string and align the result of that interpolation expression result accordingly.

Alignment

Alignment parameter will take integer value either positive or negative, if it’s positive then it will align text to the left else align text to the right.

formatString

formatString will format the string, it will support all the standard formation of the string.

Let’s see an example for the alignment parameter as below

Console.WriteLine($"{"Left Align Text",-20}");

Console.WriteLine($"{"Right Align Text",20}");

Above both instructions will produce output as below:

Left Align Text

Right Align Text

Now take a look at  an example for formatString parameter as below

decimal sampleValue = 590.1234m;
Console.WriteLine($"The above given value is {sampleValue:N2}");

Above line will produce output as below:

The above given value is 590.12

I hope this post will give you a basic idea about the string interpolation feature in C#.

Summary

This post covered C# string Interpolation which is concatenating, formatting, and manipulating strings using C# string interpolation. As part of the string interpolation operation, we can use objects and expressions. Hope you found this article helpful.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago