• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Dotnet Stuff

  • Home
  • Articles
  • Tutorials
  • Forums
  • Career Advice
  • Jobs
  • .NET FAQs
  • News
  • Privacy Policy

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);

Table of Contents

  • 1 C# String Interpolation
    • 1.1 Interpolation Expression
    • 1.2 Alignment
    • 1.3 formatString
  • 2 Summary

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.

Related posts:

  1. How To Select an XML Node Element Values According to a Specific Attribute Value – C#,VB.NET
  2. What Is The Use Of Volatile Keyword In C#
  3. SQL Data Types
  4. How To Select Required Number Of Nodes From Top or Bottom Using XPATH – C#,VB.NET

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?
Log In
Increase Your Leadership Skills, Influence, and Power with Top Leadership courses starting at just $11.99.

Recent Posts

  • .NET Core Version History | Release History of .NET Core
  • What is string interpolation in C#
  • What Is The Use Of Volatile Keyword In C#
  • Get System IP Address Using C#
  • SQL Server Interview Question and Answers
  • C# Fixed Size Buffer
  • OWIN in ASP.NET Core| What is OWIN in .NET?
  • C# Version History | C# Evolution and Features
  • What is ASP.NET Core Module
  • C# 3d array

Recent Forum Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

Forums

  • .NET Framework
  • ASP.NET
  • C#
  • SQL

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

© Copywright 2017 Dotnetstuffs All Rights Reserved