Updated April 14, 2023
Introduction to C# String Interpolation
The process of formatting, manipulating, and concatenating the strings is called string interpolation in C# using which expressions and objects can be used as a part of the operation of string interpolation. This feature of string interpolation was introduced in C# version 6 and before string interpolation was introduced + (plus) operator and String.Format method was used in C# to perform the concatenation operation on strings and by making use of string interpolation, it is possible to place the strings wherever we want them, it is possible to make use of conditions and it is possible to specify the space after or before the string.
Syntax:
The syntax for string interpolation as follows:
{<interpolatedExpression>[,<alignment>][:<formatString>]}
Where the result is produced by the interpolated expression which will be included in the interpolated string, alignment for a resulting expression can be expressed using a comma and it is optional. The resulting expression is right-aligned if the alignment value is positive. The resulting expression is left aligned if the alignment value is negative.
The given expression can be formatted by defining formatString using the colon.
Working of String Interpolation in C#
- Whenever there is a need to format, manipulate, and concatenate the strings, we make use of string interpolation in this.
- The process of formatting, manipulating, and concatenating the strings is called string interpolation in C# using which expressions and objects can be used as a part of the operation of string interpolation.
- The feature of string interpolation was introduced in C# version 6 and before string interpolation was introduced, + (plus) operator and String. Format method was used in C# to perform the concatenation operation on strings.
- By making use of string interpolation, it is possible to place the strings wherever we want them, it is possible to make use of conditions and it is possible to specify the space after or before the string.
- The resulting expression by using string interpolation can be aligned using a comma. . The resulting expression is right aligned if the alignment value is positive. The resulting expression is left aligned if the alignment value is negative.
Examples to Implement String Interpolation
Below are the examples
Example #1
program to demonstrate string interpolation to concatenate the given two strings.
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which two string variables are defined to store the two strings
static void Main(string[] args)
{
string string1 = "to C#";
//string interpolation is used to concatenate the first string with the second string and display the resulting string
string string2 = $"Welcome {string1} !";
//the resulting output which is the concatenation of the given two strings is printed on the screen
Console.WriteLine(string2);
}
}
}
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called check is defined. Then the main method is called within which two string variables are defined to store the two strings. Then string interpolation is used to concatenate the first string with the second string and display the resulting string.
Example #2
C# program to demonstrate string interpolation to concatenate the given four strings:
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which four string variables are defined to store the four strings
static void Main(string[] args)
{
string string1 = "to C#";
//string interpolation is used to concatenate the first string, second string, third string and fourth string and display the resulting string
string string2 = "Welcome";
string string3 = "Learning is fun";
string string4 = $"{string2} {string1}. \n" +
$"{string3}. ";
//the resulting output which is the concatenation of the given four strings is printed on the screen
Console.WriteLine(string4);
}
}
}
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called check is defined. Then the main method is called within which four string variables are defined to store the four strings. Then string interpolation is used to concatenate the first string, second string, third-string and fourth string and display the resulting string.
Example #3
C# program to demonstrate string interpolation to concatenate the given strings to display the email ID:
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called within which four string variables are defined to store the four strings
static void Main(string[] args)
{
string string1 = "shobha";
//string interpolation is used to concatenate the first string, second string, display the resulting string which is an email id
string string2 = "shivakumar";
string string3 = $"{string1}.{string2}@gmail.com";
//the resulting output which is an email id is printed on the screen
Console.WriteLine("The given email id after string interpolation is: {0}",string3);
}
}
}
Output:
Explanation: In the above program, a namespace called program is defined. Then a class called check is defined. Then the main method is called within which three string variables are defined to store the three strings. Then string interpolation is used to concatenate the first string, second string, display the resulting string which is an email id.
Conclusion
In this tutorial, we understand the concept of string interpolation through definition, syntax, and working of it through programming examples and their outputs.
Recommended Articles
This is a guide to C# String Interpolation. Here we discuss the Introduction of C# String Interpolation and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –